{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 游戏付费分析"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据读取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_excel('付费分析v1.0.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>玩家id</th>\n",
       "      <th>注册时间</th>\n",
       "      <th>付费时间</th>\n",
       "      <th>付费额度</th>\n",
       "      <th>计费点id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>1.99</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>9.99</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>29.99</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>19.99</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>24.99</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   玩家id        注册时间        付费时间   付费额度  计费点id\n",
       "0     1  2019-11-15  2019-11-15   1.99      3\n",
       "1     1  2019-11-15  2019-11-15   9.99      1\n",
       "2     1  2019-11-15  2019-11-16  29.99      3\n",
       "3     1  2019-11-15  2019-11-16  19.99      3\n",
       "4     1  2019-11-15  2019-11-16  24.99      3"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "data['注册时间']= pd.to_datetime(data['注册时间'])\n",
    "data['付费时间']=pd.to_datetime(data['付费时间'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 逻辑异常数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "((data['注册时间']>data['付费时间'])*1).sum()\n",
    "#存在错误数据，注册时间大于付费时间的有6组数据。需要选出。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>玩家id</th>\n",
       "      <th>注册时间</th>\n",
       "      <th>付费时间</th>\n",
       "      <th>付费额度</th>\n",
       "      <th>计费点id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>25774</th>\n",
       "      <td>819</td>\n",
       "      <td>2019-09-20</td>\n",
       "      <td>2019-08-12</td>\n",
       "      <td>29.99</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25775</th>\n",
       "      <td>819</td>\n",
       "      <td>2019-09-20</td>\n",
       "      <td>2019-08-12</td>\n",
       "      <td>29.99</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25776</th>\n",
       "      <td>819</td>\n",
       "      <td>2019-09-20</td>\n",
       "      <td>2019-08-12</td>\n",
       "      <td>29.99</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25777</th>\n",
       "      <td>819</td>\n",
       "      <td>2019-09-20</td>\n",
       "      <td>2019-08-12</td>\n",
       "      <td>29.99</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26798</th>\n",
       "      <td>852</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>2019-09-11</td>\n",
       "      <td>9.99</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31665</th>\n",
       "      <td>987</td>\n",
       "      <td>2019-11-07</td>\n",
       "      <td>2019-09-11</td>\n",
       "      <td>29.99</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       玩家id       注册时间       付费时间   付费额度  计费点id\n",
       "25774   819 2019-09-20 2019-08-12  29.99      3\n",
       "25775   819 2019-09-20 2019-08-12  29.99      3\n",
       "25776   819 2019-09-20 2019-08-12  29.99      3\n",
       "25777   819 2019-09-20 2019-08-12  29.99      3\n",
       "26798   852 2019-10-03 2019-09-11   9.99      3\n",
       "31665   987 2019-11-07 2019-09-11  29.99      3"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.loc[data['注册时间']>data['付费时间']]#=data.loc[data['注册时间']>data['付费时间'],'付费时间']\n",
    "#把付费时间赋值给注册时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.loc[data['玩家id']==819,'注册时间']=data.loc[data['玩家id']==819,'付费时间'].min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.loc[data['玩家id']==852,'注册时间']=data.loc[data['玩家id']==852,'付费时间'].min()\n",
    "data.loc[data['玩家id']==987,'注册时间']=data.loc[data['玩家id']==987,'付费时间'].min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>玩家id</th>\n",
       "      <th>注册时间</th>\n",
       "      <th>付费时间</th>\n",
       "      <th>付费额度</th>\n",
       "      <th>计费点id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>31648</th>\n",
       "      <td>987</td>\n",
       "      <td>2019-09-11</td>\n",
       "      <td>2019-11-12</td>\n",
       "      <td>19.99</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31649</th>\n",
       "      <td>987</td>\n",
       "      <td>2019-09-11</td>\n",
       "      <td>2019-11-12</td>\n",
       "      <td>0.99</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31650</th>\n",
       "      <td>987</td>\n",
       "      <td>2019-09-11</td>\n",
       "      <td>2019-11-12</td>\n",
       "      <td>0.99</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31651</th>\n",
       "      <td>987</td>\n",
       "      <td>2019-09-11</td>\n",
       "      <td>2019-11-12</td>\n",
       "      <td>0.99</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31652</th>\n",
       "      <td>987</td>\n",
       "      <td>2019-09-11</td>\n",
       "      <td>2019-11-13</td>\n",
       "      <td>3.99</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31653</th>\n",
       "      <td>987</td>\n",
       "      <td>2019-09-11</td>\n",
       "      <td>2019-11-13</td>\n",
       "      <td>2.99</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31654</th>\n",
       "      <td>987</td>\n",
       "      <td>2019-09-11</td>\n",
       "      <td>2019-11-13</td>\n",
       "      <td>1.99</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31655</th>\n",
       "      <td>987</td>\n",
       "      <td>2019-09-11</td>\n",
       "      <td>2019-11-13</td>\n",
       "      <td>2.99</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31656</th>\n",
       "      <td>987</td>\n",
       "      <td>2019-09-11</td>\n",
       "      <td>2019-11-18</td>\n",
       "      <td>3.99</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31657</th>\n",
       "      <td>987</td>\n",
       "      <td>2019-09-11</td>\n",
       "      <td>2019-11-18</td>\n",
       "      <td>2.99</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31658</th>\n",
       "      <td>987</td>\n",
       "      <td>2019-09-11</td>\n",
       "      <td>2019-11-07</td>\n",
       "      <td>4.99</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31659</th>\n",
       "      <td>987</td>\n",
       "      <td>2019-09-11</td>\n",
       "      <td>2019-11-07</td>\n",
       "      <td>0.99</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31660</th>\n",
       "      <td>987</td>\n",
       "      <td>2019-09-11</td>\n",
       "      <td>2019-11-07</td>\n",
       "      <td>9.99</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31661</th>\n",
       "      <td>987</td>\n",
       "      <td>2019-09-11</td>\n",
       "      <td>2019-11-07</td>\n",
       "      <td>2.99</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31662</th>\n",
       "      <td>987</td>\n",
       "      <td>2019-09-11</td>\n",
       "      <td>2019-11-07</td>\n",
       "      <td>0.99</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31663</th>\n",
       "      <td>987</td>\n",
       "      <td>2019-09-11</td>\n",
       "      <td>2019-11-09</td>\n",
       "      <td>0.99</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31664</th>\n",
       "      <td>987</td>\n",
       "      <td>2019-09-11</td>\n",
       "      <td>2019-11-09</td>\n",
       "      <td>0.99</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31665</th>\n",
       "      <td>987</td>\n",
       "      <td>2019-09-11</td>\n",
       "      <td>2019-09-11</td>\n",
       "      <td>29.99</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       玩家id       注册时间       付费时间   付费额度  计费点id\n",
       "31648   987 2019-09-11 2019-11-12  19.99      1\n",
       "31649   987 2019-09-11 2019-11-12   0.99      2\n",
       "31650   987 2019-09-11 2019-11-12   0.99      2\n",
       "31651   987 2019-09-11 2019-11-12   0.99      2\n",
       "31652   987 2019-09-11 2019-11-13   3.99      3\n",
       "31653   987 2019-09-11 2019-11-13   2.99      2\n",
       "31654   987 2019-09-11 2019-11-13   1.99      3\n",
       "31655   987 2019-09-11 2019-11-13   2.99      2\n",
       "31656   987 2019-09-11 2019-11-18   3.99      3\n",
       "31657   987 2019-09-11 2019-11-18   2.99      1\n",
       "31658   987 2019-09-11 2019-11-07   4.99      2\n",
       "31659   987 2019-09-11 2019-11-07   0.99      2\n",
       "31660   987 2019-09-11 2019-11-07   9.99      2\n",
       "31661   987 2019-09-11 2019-11-07   2.99      2\n",
       "31662   987 2019-09-11 2019-11-07   0.99      2\n",
       "31663   987 2019-09-11 2019-11-09   0.99      2\n",
       "31664   987 2019-09-11 2019-11-09   0.99      2\n",
       "31665   987 2019-09-11 2019-09-11  29.99      3"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.loc[data['玩家id']==987]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 按照付费时间重新排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "data1 = data.sort_values(by = ['玩家id','付费时间']).reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 构造“What”维度"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 首天付费"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>玩家id</th>\n",
       "      <th>付费时间</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>2019-08-13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>2019-12-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>2019-11-25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>2019-08-11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>996</td>\n",
       "      <td>2019-09-12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>997</td>\n",
       "      <td>2019-02-07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>998</td>\n",
       "      <td>2019-11-26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>999</td>\n",
       "      <td>2019-08-18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>1000</td>\n",
       "      <td>2019-06-15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     玩家id       付费时间\n",
       "0       1 2019-11-15\n",
       "1       2 2019-08-13\n",
       "2       3 2019-12-03\n",
       "3       4 2019-11-25\n",
       "4       5 2019-08-11\n",
       "..    ...        ...\n",
       "995   996 2019-09-12\n",
       "996   997 2019-02-07\n",
       "997   998 2019-11-26\n",
       "998   999 2019-08-18\n",
       "999  1000 2019-06-15\n",
       "\n",
       "[1000 rows x 2 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1.groupby('玩家id',as_index=False)['付费时间'].min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_first_day_pay = data1.groupby('玩家id',as_index=False)['付费时间'].min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_first_day_pay['首天付费']= 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "data2 = pd.merge(left=data1,right=data_first_day_pay,how ='outer')\n",
    "data2.fillna(0,inplace = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>玩家id</th>\n",
       "      <th>注册时间</th>\n",
       "      <th>付费时间</th>\n",
       "      <th>付费额度</th>\n",
       "      <th>计费点id</th>\n",
       "      <th>首天付费</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>1.99</td>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>9.99</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>29.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>19.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>24.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31941</th>\n",
       "      <td>1000</td>\n",
       "      <td>2019-06-14</td>\n",
       "      <td>2019-09-22</td>\n",
       "      <td>9.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31942</th>\n",
       "      <td>1000</td>\n",
       "      <td>2019-06-14</td>\n",
       "      <td>2019-09-24</td>\n",
       "      <td>9.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31943</th>\n",
       "      <td>1000</td>\n",
       "      <td>2019-06-14</td>\n",
       "      <td>2019-09-24</td>\n",
       "      <td>9.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31944</th>\n",
       "      <td>1000</td>\n",
       "      <td>2019-06-14</td>\n",
       "      <td>2019-10-22</td>\n",
       "      <td>9.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31945</th>\n",
       "      <td>1000</td>\n",
       "      <td>2019-06-14</td>\n",
       "      <td>2020-03-21</td>\n",
       "      <td>9.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>31946 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       玩家id       注册时间       付费时间   付费额度  计费点id  首天付费\n",
       "0         1 2019-11-15 2019-11-15   1.99      3   1.0\n",
       "1         1 2019-11-15 2019-11-15   9.99      1   1.0\n",
       "2         1 2019-11-15 2019-11-16  29.99      3   0.0\n",
       "3         1 2019-11-15 2019-11-16  19.99      3   0.0\n",
       "4         1 2019-11-15 2019-11-16  24.99      3   0.0\n",
       "...     ...        ...        ...    ...    ...   ...\n",
       "31941  1000 2019-06-14 2019-09-22   9.99      3   0.0\n",
       "31942  1000 2019-06-14 2019-09-24   9.99      3   0.0\n",
       "31943  1000 2019-06-14 2019-09-24   9.99      3   0.0\n",
       "31944  1000 2019-06-14 2019-10-22   9.99      3   0.0\n",
       "31945  1000 2019-06-14 2020-03-21   9.99      3   0.0\n",
       "\n",
       "[31946 rows x 6 columns]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 首次付费"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([    0,    34,    44,   209,   232,   351,   406,   450,   463,\n",
       "         497,   515,   708,   725,   738,   770,   808,   823,   907,\n",
       "         926,   999,  1029,  1054,  1104,  1122,  1142,  1152,  1169,\n",
       "        1179,  1204,  1215,  1339,  1356,  1385,  1395,  1412,  1447,\n",
       "        1477,  1510,  1521,  1536,  1638,  1650,  1663,  1798,  1824,\n",
       "        1857,  1868,  1882,  1896,  1998,  2016,  2027,  2042,  2153,\n",
       "        2163,  2199,  2211,  2226,  2238,  2331,  2356,  2416,  2441,\n",
       "        2458,  2488,  2507,  2519,  2532,  2551,  2565,  2584,  2594,\n",
       "        2606,  2643,  2653,  2680,  2704,  2715,  2727,  2742,  2755,\n",
       "        2776,  2827,  2838,  2855,  2866,  2878,  2923,  3002,  3037,\n",
       "        3051,  3095,  3106,  3132,  3153,  3400,  3410,  3422,  3557,\n",
       "        3586,  3627,  3667,  3833,  3984,  4111,  4152,  4174,  4236,\n",
       "        4251,  4282,  4298,  4311,  4328,  4365,  4375,  4398,  4409,\n",
       "        4435,  4465,  4475,  4498,  4508,  4544,  4562,  4610,  4685,\n",
       "        4698,  4708,  4723,  4742,  4767,  4785,  4811,  4821,  4841,\n",
       "        4852,  4877,  4887,  4912,  4922,  4933,  4954,  4984,  5028,\n",
       "        5054,  5084,  5098,  5108,  5120,  5133,  5183,  5195,  5207,\n",
       "        5222,  5237,  5283,  5295,  5306,  5320,  5653,  5668,  5817,\n",
       "        5863,  5875,  5987,  6003,  6017,  6033,  6053,  6068,  6087,\n",
       "        6228,  6254,  6266,  6277,  6291,  6308,  6330,  6342,  6370,\n",
       "        6382,  6394,  6428,  6454,  6509,  6524,  6534,  6547,  6561,\n",
       "        6571,  6585,  6634,  6653,  6663,  6676,  6697,  6707,  6717,\n",
       "        6731,  6743,  6756,  6777,  6796,  6806,  6817,  6828,  6841,\n",
       "        6854,  6885,  6946,  6962,  7492,  7506,  7526,  7556,  7576,\n",
       "        7643,  7655,  7687,  7719,  7765,  7892,  8007,  8023,  8038,\n",
       "        8052,  8062,  8076,  8093,  8114,  8136,  8174,  8240,  8598,\n",
       "        8638,  8657,  8671,  8692,  8715,  8739,  8769,  8791,  8802,\n",
       "        8817,  8862,  8891,  8927,  8957,  8967,  8977,  9017,  9043,\n",
       "        9074,  9085,  9099,  9122,  9156,  9172,  9183,  9195,  9260,\n",
       "        9279,  9289,  9337,  9355,  9397,  9413,  9524,  9563,  9581,\n",
       "        9599,  9610,  9630,  9642,  9772,  9783,  9803,  9819,  9900,\n",
       "        9935,  9947,  9989, 10006, 10018, 10069, 10116, 10154, 10182,\n",
       "       10218, 10231, 10274, 10291, 10324, 10337, 10349, 10375, 10388,\n",
       "       10413, 10423, 10511, 10533, 10550, 10561, 10575, 10595, 10622,\n",
       "       10638, 10654, 10696, 10719, 10729, 10766, 10794, 10820, 10831,\n",
       "       10849, 10892, 10931, 10941, 10965, 10979, 10997, 11008, 11035,\n",
       "       11045, 11103, 11128, 11165, 11189, 11255, 11323, 11415, 11431,\n",
       "       11462, 11510, 11633, 11645, 11666, 11679, 11690, 11703, 11852,\n",
       "       11866, 11883, 11912, 11922, 11954, 11982, 11992, 12009, 12020,\n",
       "       12030, 12046, 12077, 12087, 12103, 12116, 12130, 12145, 12157,\n",
       "       12184, 12228, 12299, 12314, 12340, 12352, 12392, 12409, 12419,\n",
       "       12562, 12579, 12589, 12604, 12615, 12636, 12659, 12672, 12685,\n",
       "       12709, 12723, 12733, 12750, 12772, 12795, 12863, 12887, 12906,\n",
       "       12924, 13086, 13098, 13111, 13126, 13149, 13182, 13200, 13249,\n",
       "       13264, 13274, 13306, 13347, 13357, 13378, 13523, 13537, 13572,\n",
       "       13601, 13626, 13643, 13656, 13673, 13705, 13715, 13732, 13750,\n",
       "       13772, 13785, 13797, 13835, 13846, 13858, 14128, 14157, 14356,\n",
       "       14378, 14397, 14407, 14422, 14445, 14457, 14471, 14486, 14521,\n",
       "       14542, 14556, 14571, 14589, 14618, 14633, 14647, 14665, 14681,\n",
       "       14695, 14736, 14749, 14769, 14799, 14811, 14885, 14910, 14924,\n",
       "       14940, 14959, 14979, 15010, 15025, 15036, 15159, 15193, 15211,\n",
       "       15233, 15243, 15285, 15297, 15325, 15346, 15356, 15373, 15394,\n",
       "       15417, 15428, 15445, 15501, 15543, 15575, 15600, 15614, 15624,\n",
       "       15634, 15644, 15659, 15669, 15679, 15708, 15735, 15758, 15773,\n",
       "       15811, 15822, 15835, 15847, 15889, 15902, 15924, 15934, 15956,\n",
       "       15968, 15996, 16009, 16154, 16165, 16197, 16211, 16232, 16264,\n",
       "       16281, 16301, 16315, 16327, 16348, 16361, 16378, 16398, 16410,\n",
       "       16430, 16458, 16502, 16519, 16534, 16552, 16566, 16617, 16632,\n",
       "       16651, 16671, 16688, 16709, 16750, 16762, 16791, 16805, 16816,\n",
       "       16827, 16891, 16906, 16917, 17105, 17125, 17153, 17163, 17184,\n",
       "       17194, 17204, 17233, 17247, 17276, 17296, 17308, 17322, 17350,\n",
       "       17504, 17516, 17528, 17609, 17629, 17644, 17711, 17773, 17789,\n",
       "       17810, 17823, 17841, 17869, 18011, 18111, 18124, 18147, 18160,\n",
       "       18171, 18184, 18195, 18205, 18255, 18314, 18359, 18384, 18402,\n",
       "       18420, 18441, 18453, 18469, 18497, 18548, 18609, 18641, 18666,\n",
       "       18700, 18720, 18756, 18797, 18869, 18907, 18917, 18929, 18955,\n",
       "       18995, 19011, 19049, 19072, 19090, 19101, 19171, 19707, 19754,\n",
       "       19768, 19785, 19803, 19815, 19825, 19837, 19847, 19863, 19879,\n",
       "       19892, 19904, 19915, 19927, 20017, 20029, 20062, 20109, 20135,\n",
       "       20176, 20240, 20322, 20332, 20346, 20358, 20368, 20398, 20411,\n",
       "       20439, 20450, 20463, 20480, 20496, 20512, 20643, 20659, 20669,\n",
       "       20681, 20691, 20707, 20748, 20779, 20802, 20825, 20844, 20857,\n",
       "       20868, 20879, 20898, 20908, 20924, 20952, 21141, 21164, 21190,\n",
       "       21205, 21217, 21237, 21248, 21259, 21277, 21316, 21339, 21355,\n",
       "       21368, 21393, 21416, 21439, 21455, 21475, 21507, 21543, 21556,\n",
       "       21571, 21625, 21647, 21685, 21758, 21794, 21805, 21819, 21839,\n",
       "       21860, 21889, 21904, 21978, 22039, 22058, 22072, 22111, 22124,\n",
       "       22166, 22185, 22209, 22230, 22279, 22298, 22308, 22336, 22372,\n",
       "       22384, 22415, 22498, 22517, 22558, 22568, 22589, 22627, 22663,\n",
       "       22676, 22687, 22698, 22715, 22735, 22746, 22758, 22771, 22831,\n",
       "       22965, 22990, 23003, 23013, 23029, 23076, 23087, 23106, 23131,\n",
       "       23143, 23175, 23192, 23479, 23518, 23573, 23586, 23676, 23693,\n",
       "       23705, 23723, 23738, 23750, 23778, 23802, 23865, 23876, 23890,\n",
       "       23916, 23946, 23986, 23997, 24008, 24026, 24036, 24061, 24085,\n",
       "       24102, 24118, 24129, 24166, 24183, 24204, 24217, 24244, 24259,\n",
       "       24273, 24285, 24313, 24358, 24387, 24454, 24467, 24483, 24493,\n",
       "       24517, 24556, 24592, 24627, 24657, 24676, 24689, 24755, 24799,\n",
       "       24810, 24826, 24841, 24869, 24892, 24963, 24977, 24993, 25029,\n",
       "       25043, 25054, 25123, 25145, 25158, 25172, 25197, 25219, 25229,\n",
       "       25247, 25303, 25333, 25359, 25369, 25383, 25450, 25497, 25536,\n",
       "       25548, 25561, 25571, 25586, 25599, 25622, 25645, 25663, 25725,\n",
       "       25781, 25796, 25806, 25825, 25845, 25886, 25905, 25916, 26008,\n",
       "       26021, 26037, 26047, 26116, 26128, 26140, 26157, 26168, 26183,\n",
       "       26207, 26253, 26273, 26296, 26334, 26464, 26493, 26548, 26576,\n",
       "       26589, 26604, 26616, 26647, 26741, 26784, 26808, 26827, 27020,\n",
       "       27048, 27061, 27110, 27133, 27145, 27162, 27199, 27209, 27219,\n",
       "       27254, 27269, 27314, 27324, 27341, 27359, 27374, 27384, 27414,\n",
       "       27424, 27465, 27479, 27512, 27533, 27546, 27568, 27585, 27646,\n",
       "       27666, 27797, 27833, 27858, 27875, 27885, 28045, 28059, 28069,\n",
       "       28080, 28116, 28131, 28158, 28168, 28179, 28200, 28212, 28240,\n",
       "       28256, 28268, 28278, 28300, 28315, 28342, 28353, 28368, 28380,\n",
       "       28398, 28413, 28435, 28505, 28566, 28579, 28639, 28651, 28691,\n",
       "       28703, 28719, 28735, 28763, 28773, 28792, 28814, 28824, 28835,\n",
       "       28848, 28864, 28892, 28916, 28948, 28965, 29055, 29065, 29076,\n",
       "       29124, 29138, 29246, 29269, 29299, 29987, 30029, 30269, 30290,\n",
       "       30301, 30319, 30338, 30355, 30371, 30382, 30416, 30432, 30450,\n",
       "       30480, 30514, 30526, 30731, 30749, 30796, 30806, 30817, 30863,\n",
       "       30874, 30888, 30911, 30964, 31155, 31176, 31189, 31236, 31248,\n",
       "       31259, 31276, 31286, 31326, 31338, 31359, 31390, 31429, 31442,\n",
       "       31471, 31494, 31508, 31572, 31635, 31648, 31666, 31681, 31705,\n",
       "       31719, 31756, 31767, 31794, 31814, 31824, 31834, 31897, 31910,\n",
       "       31920], dtype=int64)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2[data2['首天付费']==1].reset_index().groupby('玩家id')['index'].min().values\n",
    "#找到首次付费的index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "firt_pay_index = data2[data2['首天付费']==1].reset_index().groupby('玩家id')['index'].min().values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "data2['首次付费']=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "data2.loc[firt_pay_index,'首次付费']=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>玩家id</th>\n",
       "      <th>注册时间</th>\n",
       "      <th>付费时间</th>\n",
       "      <th>付费额度</th>\n",
       "      <th>计费点id</th>\n",
       "      <th>首天付费</th>\n",
       "      <th>首次付费</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>1.99</td>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>9.99</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>29.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>19.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>24.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31941</th>\n",
       "      <td>1000</td>\n",
       "      <td>2019-06-14</td>\n",
       "      <td>2019-09-22</td>\n",
       "      <td>9.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31942</th>\n",
       "      <td>1000</td>\n",
       "      <td>2019-06-14</td>\n",
       "      <td>2019-09-24</td>\n",
       "      <td>9.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31943</th>\n",
       "      <td>1000</td>\n",
       "      <td>2019-06-14</td>\n",
       "      <td>2019-09-24</td>\n",
       "      <td>9.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31944</th>\n",
       "      <td>1000</td>\n",
       "      <td>2019-06-14</td>\n",
       "      <td>2019-10-22</td>\n",
       "      <td>9.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31945</th>\n",
       "      <td>1000</td>\n",
       "      <td>2019-06-14</td>\n",
       "      <td>2020-03-21</td>\n",
       "      <td>9.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>31946 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       玩家id       注册时间       付费时间   付费额度  计费点id  首天付费  首次付费\n",
       "0         1 2019-11-15 2019-11-15   1.99      3   1.0     1\n",
       "1         1 2019-11-15 2019-11-15   9.99      1   1.0     0\n",
       "2         1 2019-11-15 2019-11-16  29.99      3   0.0     0\n",
       "3         1 2019-11-15 2019-11-16  19.99      3   0.0     0\n",
       "4         1 2019-11-15 2019-11-16  24.99      3   0.0     0\n",
       "...     ...        ...        ...    ...    ...   ...   ...\n",
       "31941  1000 2019-06-14 2019-09-22   9.99      3   0.0     0\n",
       "31942  1000 2019-06-14 2019-09-24   9.99      3   0.0     0\n",
       "31943  1000 2019-06-14 2019-09-24   9.99      3   0.0     0\n",
       "31944  1000 2019-06-14 2019-10-22   9.99      3   0.0     0\n",
       "31945  1000 2019-06-14 2020-03-21   9.99      3   0.0     0\n",
       "\n",
       "[31946 rows x 7 columns]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 已经游玩的天数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "data2['游玩天数']=(data2['付费时间']-data2['注册时间']).dt.days"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>玩家id</th>\n",
       "      <th>注册时间</th>\n",
       "      <th>付费时间</th>\n",
       "      <th>付费额度</th>\n",
       "      <th>计费点id</th>\n",
       "      <th>首天付费</th>\n",
       "      <th>首次付费</th>\n",
       "      <th>游玩天数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>1.99</td>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>9.99</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>29.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>19.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>24.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   玩家id       注册时间       付费时间   付费额度  计费点id  首天付费  首次付费  游玩天数\n",
       "0     1 2019-11-15 2019-11-15   1.99      3   1.0     1     0\n",
       "1     1 2019-11-15 2019-11-15   9.99      1   1.0     0     0\n",
       "2     1 2019-11-15 2019-11-16  29.99      3   0.0     0     1\n",
       "3     1 2019-11-15 2019-11-16  19.99      3   0.0     0     1\n",
       "4     1 2019-11-15 2019-11-16  24.99      3   0.0     0     1"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 构造“When”维度"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 月份"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "data2['月份']=data2['付费时间'].dt.month"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 星期"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "data2['星期']=data2['付费时间'].dt.weekday\n",
    "#0：星期一 \n",
    "#1：星期二"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 是否是周末"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "data2['是否是周末']=data2['星期'].apply(lambda x:0 if x<=3 else 1 )\n",
    "#星期五-星期日：4-6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>玩家id</th>\n",
       "      <th>注册时间</th>\n",
       "      <th>付费时间</th>\n",
       "      <th>付费额度</th>\n",
       "      <th>计费点id</th>\n",
       "      <th>首天付费</th>\n",
       "      <th>首次付费</th>\n",
       "      <th>游玩天数</th>\n",
       "      <th>月份</th>\n",
       "      <th>星期</th>\n",
       "      <th>是否是周末</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>1.99</td>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>9.99</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>29.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>19.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>24.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   玩家id       注册时间       付费时间   付费额度  计费点id  首天付费  首次付费  游玩天数  月份  星期  是否是周末\n",
       "0     1 2019-11-15 2019-11-15   1.99      3   1.0     1     0  11   4      1\n",
       "1     1 2019-11-15 2019-11-15   9.99      1   1.0     0     0  11   4      1\n",
       "2     1 2019-11-15 2019-11-16  29.99      3   0.0     0     1  11   5      1\n",
       "3     1 2019-11-15 2019-11-16  19.99      3   0.0     0     1  11   5      1\n",
       "4     1 2019-11-15 2019-11-16  24.99      3   0.0     0     1  11   5      1"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 距上次的付费的天数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "data3 = data2.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "data3['上次付费时间']=data3.groupby('玩家id')['付费时间'].shift(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "data3['距上次付费的天数']=(data3['付费时间']-data3['上次付费时间']).dt.days"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>玩家id</th>\n",
       "      <th>注册时间</th>\n",
       "      <th>付费时间</th>\n",
       "      <th>付费额度</th>\n",
       "      <th>计费点id</th>\n",
       "      <th>首天付费</th>\n",
       "      <th>首次付费</th>\n",
       "      <th>游玩天数</th>\n",
       "      <th>月份</th>\n",
       "      <th>星期</th>\n",
       "      <th>是否是周末</th>\n",
       "      <th>上次付费时间</th>\n",
       "      <th>距上次付费的天数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>1.99</td>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>NaT</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>9.99</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>29.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>19.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>24.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   玩家id       注册时间       付费时间   付费额度  计费点id  首天付费  首次付费  游玩天数  月份  星期  是否是周末  \\\n",
       "0     1 2019-11-15 2019-11-15   1.99      3   1.0     1     0  11   4      1   \n",
       "1     1 2019-11-15 2019-11-15   9.99      1   1.0     0     0  11   4      1   \n",
       "2     1 2019-11-15 2019-11-16  29.99      3   0.0     0     1  11   5      1   \n",
       "3     1 2019-11-15 2019-11-16  19.99      3   0.0     0     1  11   5      1   \n",
       "4     1 2019-11-15 2019-11-16  24.99      3   0.0     0     1  11   5      1   \n",
       "\n",
       "      上次付费时间  距上次付费的天数  \n",
       "0        NaT       NaN  \n",
       "1 2019-11-15       0.0  \n",
       "2 2019-11-15       1.0  \n",
       "3 2019-11-16       0.0  \n",
       "4 2019-11-16       0.0  "
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data3.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>玩家id</th>\n",
       "      <th>注册时间</th>\n",
       "      <th>付费时间</th>\n",
       "      <th>付费额度</th>\n",
       "      <th>计费点id</th>\n",
       "      <th>首天付费</th>\n",
       "      <th>首次付费</th>\n",
       "      <th>游玩天数</th>\n",
       "      <th>月份</th>\n",
       "      <th>星期</th>\n",
       "      <th>是否是周末</th>\n",
       "      <th>上次付费时间</th>\n",
       "      <th>距上次付费的天数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>1.99</td>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>9.99</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15 00:00:00</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>29.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15 00:00:00</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>19.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-16 00:00:00</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>24.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-16 00:00:00</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   玩家id       注册时间       付费时间   付费额度  计费点id  首天付费  首次付费  游玩天数  月份  星期  是否是周末  \\\n",
       "0     1 2019-11-15 2019-11-15   1.99      3   1.0     1     0  11   4      1   \n",
       "1     1 2019-11-15 2019-11-15   9.99      1   1.0     0     0  11   4      1   \n",
       "2     1 2019-11-15 2019-11-16  29.99      3   0.0     0     1  11   5      1   \n",
       "3     1 2019-11-15 2019-11-16  19.99      3   0.0     0     1  11   5      1   \n",
       "4     1 2019-11-15 2019-11-16  24.99      3   0.0     0     1  11   5      1   \n",
       "\n",
       "                上次付费时间  距上次付费的天数  \n",
       "0                    0       0.0  \n",
       "1  2019-11-15 00:00:00       0.0  \n",
       "2  2019-11-15 00:00:00       1.0  \n",
       "3  2019-11-16 00:00:00       0.0  \n",
       "4  2019-11-16 00:00:00       0.0  "
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data3.fillna(0,inplace=True)\n",
    "data3.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>玩家id</th>\n",
       "      <th>注册时间</th>\n",
       "      <th>付费时间</th>\n",
       "      <th>付费额度</th>\n",
       "      <th>计费点id</th>\n",
       "      <th>首天付费</th>\n",
       "      <th>首次付费</th>\n",
       "      <th>游玩天数</th>\n",
       "      <th>月份</th>\n",
       "      <th>星期</th>\n",
       "      <th>是否是周末</th>\n",
       "      <th>距上次付费的天数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>1.99</td>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>9.99</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>29.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>19.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>24.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   玩家id       注册时间       付费时间   付费额度  计费点id  首天付费  首次付费  游玩天数  月份  星期  是否是周末  \\\n",
       "0     1 2019-11-15 2019-11-15   1.99      3   1.0     1     0  11   4      1   \n",
       "1     1 2019-11-15 2019-11-15   9.99      1   1.0     0     0  11   4      1   \n",
       "2     1 2019-11-15 2019-11-16  29.99      3   0.0     0     1  11   5      1   \n",
       "3     1 2019-11-15 2019-11-16  19.99      3   0.0     0     1  11   5      1   \n",
       "4     1 2019-11-15 2019-11-16  24.99      3   0.0     0     1  11   5      1   \n",
       "\n",
       "   距上次付费的天数  \n",
       "0       0.0  \n",
       "1       0.0  \n",
       "2       1.0  \n",
       "3       0.0  \n",
       "4       0.0  "
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data3.drop(labels='上次付费时间',axis=1,inplace=True)\n",
    "data3.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 构造“Who”维度"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 累计消费额"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "data3['累计消费额']=data3.groupby('玩家id')['付费额度'].cumsum()-data3['付费额度']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 累计订单数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "data3['累计订单数']=data3.groupby('玩家id')['付费额度'].cumcount()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 均单价"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "data3['均单价']=(data3['累计消费额']/data3['累计订单数']).fillna(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>玩家id</th>\n",
       "      <th>注册时间</th>\n",
       "      <th>付费时间</th>\n",
       "      <th>付费额度</th>\n",
       "      <th>计费点id</th>\n",
       "      <th>首天付费</th>\n",
       "      <th>首次付费</th>\n",
       "      <th>游玩天数</th>\n",
       "      <th>月份</th>\n",
       "      <th>星期</th>\n",
       "      <th>是否是周末</th>\n",
       "      <th>距上次付费的天数</th>\n",
       "      <th>累计消费额</th>\n",
       "      <th>累计订单数</th>\n",
       "      <th>均单价</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>1.99</td>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>9.99</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.99</td>\n",
       "      <td>1</td>\n",
       "      <td>1.99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>29.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>11.98</td>\n",
       "      <td>2</td>\n",
       "      <td>5.99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>19.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>41.97</td>\n",
       "      <td>3</td>\n",
       "      <td>13.99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>24.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>61.96</td>\n",
       "      <td>4</td>\n",
       "      <td>15.49</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   玩家id       注册时间       付费时间   付费额度  计费点id  首天付费  首次付费  游玩天数  月份  星期  是否是周末  \\\n",
       "0     1 2019-11-15 2019-11-15   1.99      3   1.0     1     0  11   4      1   \n",
       "1     1 2019-11-15 2019-11-15   9.99      1   1.0     0     0  11   4      1   \n",
       "2     1 2019-11-15 2019-11-16  29.99      3   0.0     0     1  11   5      1   \n",
       "3     1 2019-11-15 2019-11-16  19.99      3   0.0     0     1  11   5      1   \n",
       "4     1 2019-11-15 2019-11-16  24.99      3   0.0     0     1  11   5      1   \n",
       "\n",
       "   距上次付费的天数  累计消费额  累计订单数    均单价  \n",
       "0       0.0   0.00      0   0.00  \n",
       "1       0.0   1.99      1   1.99  \n",
       "2       1.0  11.98      2   5.99  \n",
       "3       0.0  41.97      3  13.99  \n",
       "4       0.0  61.96      4  15.49  "
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data3.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 构造“Where”维度"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 上次的计费点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "data3['上次的计费点'] = data3['计费点id'].shift(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "data3.loc[data3['首次付费']==1,'上次的计费点']= 0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 最高频计费点(4个）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "# import seaborn as sns\n",
    "# sns.heatmap(data3.corr())\n",
    "# plt.rcParams['font.sans-serif']=['SimHei'] # 用来正常显示中文标签\n",
    "# plt.rcParams['axes.unicode_minus']=False # 用来正常显示负号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n",
      "11\n",
      "12\n",
      "13\n",
      "14\n",
      "15\n",
      "16\n",
      "17\n",
      "18\n",
      "19\n",
      "20\n",
      "21\n",
      "22\n",
      "23\n",
      "24\n",
      "25\n",
      "26\n",
      "27\n",
      "28\n",
      "29\n",
      "30\n",
      "31\n",
      "32\n",
      "33\n",
      "34\n",
      "35\n",
      "36\n",
      "37\n",
      "38\n",
      "39\n",
      "40\n",
      "41\n",
      "42\n",
      "43\n",
      "44\n",
      "45\n",
      "46\n",
      "47\n",
      "48\n",
      "49\n",
      "50\n",
      "51\n",
      "52\n",
      "53\n",
      "54\n",
      "55\n",
      "56\n",
      "57\n",
      "58\n",
      "59\n",
      "60\n",
      "61\n",
      "62\n",
      "63\n",
      "64\n",
      "65\n",
      "66\n",
      "67\n",
      "68\n",
      "69\n",
      "70\n",
      "71\n",
      "72\n",
      "73\n",
      "74\n",
      "75\n",
      "76\n",
      "77\n",
      "78\n",
      "79\n",
      "80\n",
      "81\n",
      "82\n",
      "83\n",
      "84\n",
      "85\n",
      "86\n",
      "87\n",
      "88\n",
      "89\n",
      "90\n",
      "91\n",
      "92\n",
      "93\n",
      "94\n",
      "95\n",
      "96\n",
      "97\n",
      "98\n",
      "99\n",
      "100\n",
      "101\n",
      "102\n",
      "103\n",
      "104\n",
      "105\n",
      "106\n",
      "107\n",
      "108\n",
      "109\n",
      "110\n",
      "111\n",
      "112\n",
      "113\n",
      "114\n",
      "115\n",
      "116\n",
      "117\n",
      "118\n",
      "119\n",
      "120\n",
      "121\n",
      "122\n",
      "123\n",
      "124\n",
      "125\n",
      "126\n",
      "127\n",
      "128\n",
      "129\n",
      "130\n",
      "131\n",
      "132\n",
      "133\n",
      "134\n",
      "135\n",
      "136\n",
      "137\n",
      "138\n",
      "139\n",
      "140\n",
      "141\n",
      "142\n",
      "143\n",
      "144\n",
      "145\n",
      "146\n",
      "147\n",
      "148\n",
      "149\n",
      "150\n",
      "151\n",
      "152\n",
      "153\n",
      "154\n",
      "155\n",
      "156\n",
      "157\n",
      "158\n",
      "159\n",
      "160\n",
      "161\n",
      "162\n",
      "163\n",
      "164\n",
      "165\n",
      "166\n",
      "167\n",
      "168\n",
      "169\n",
      "170\n",
      "171\n",
      "172\n",
      "173\n",
      "174\n",
      "175\n",
      "176\n",
      "177\n",
      "178\n",
      "179\n",
      "180\n",
      "181\n",
      "182\n",
      "183\n",
      "184\n",
      "185\n",
      "186\n",
      "187\n",
      "188\n",
      "189\n",
      "190\n",
      "191\n",
      "192\n",
      "193\n",
      "194\n",
      "195\n",
      "196\n",
      "197\n",
      "198\n",
      "199\n",
      "200\n",
      "201\n",
      "202\n",
      "203\n",
      "204\n",
      "205\n",
      "206\n",
      "207\n",
      "208\n",
      "209\n",
      "210\n",
      "211\n",
      "212\n",
      "213\n",
      "214\n",
      "215\n",
      "216\n",
      "217\n",
      "218\n",
      "219\n",
      "220\n",
      "221\n",
      "222\n",
      "223\n",
      "224\n",
      "225\n",
      "226\n",
      "227\n",
      "228\n",
      "229\n",
      "230\n",
      "231\n",
      "232\n",
      "233\n",
      "234\n",
      "235\n",
      "236\n",
      "237\n",
      "238\n",
      "239\n",
      "240\n",
      "241\n",
      "242\n",
      "243\n",
      "244\n",
      "245\n",
      "246\n",
      "247\n",
      "248\n",
      "249\n",
      "250\n",
      "251\n",
      "252\n",
      "253\n",
      "254\n",
      "255\n",
      "256\n",
      "257\n",
      "258\n",
      "259\n",
      "260\n",
      "261\n",
      "262\n",
      "263\n",
      "264\n",
      "265\n",
      "266\n",
      "267\n",
      "268\n",
      "269\n",
      "270\n",
      "271\n",
      "272\n",
      "273\n",
      "274\n",
      "275\n",
      "276\n",
      "277\n",
      "278\n",
      "279\n",
      "280\n",
      "281\n",
      "282\n",
      "283\n",
      "284\n",
      "285\n",
      "286\n",
      "287\n",
      "288\n",
      "289\n",
      "290\n",
      "291\n",
      "292\n",
      "293\n",
      "294\n",
      "295\n",
      "296\n",
      "297\n",
      "298\n",
      "299\n",
      "300\n",
      "301\n",
      "302\n",
      "303\n",
      "304\n",
      "305\n",
      "306\n",
      "307\n",
      "308\n",
      "309\n",
      "310\n",
      "311\n",
      "312\n",
      "313\n",
      "314\n",
      "315\n",
      "316\n",
      "317\n",
      "318\n",
      "319\n",
      "320\n",
      "321\n",
      "322\n",
      "323\n",
      "324\n",
      "325\n",
      "326\n",
      "327\n",
      "328\n",
      "329\n",
      "330\n",
      "331\n",
      "332\n",
      "333\n",
      "334\n",
      "335\n",
      "336\n",
      "337\n",
      "338\n",
      "339\n",
      "340\n",
      "341\n",
      "342\n",
      "343\n",
      "344\n",
      "345\n",
      "346\n",
      "347\n",
      "348\n",
      "349\n",
      "350\n",
      "351\n",
      "352\n",
      "353\n",
      "354\n",
      "355\n",
      "356\n",
      "357\n",
      "358\n",
      "359\n",
      "360\n",
      "361\n",
      "362\n",
      "363\n",
      "364\n",
      "365\n",
      "366\n",
      "367\n",
      "368\n",
      "369\n",
      "370\n",
      "371\n",
      "372\n",
      "373\n",
      "374\n",
      "375\n",
      "376\n",
      "377\n",
      "378\n",
      "379\n",
      "380\n",
      "381\n",
      "382\n",
      "383\n",
      "384\n",
      "385\n",
      "386\n",
      "387\n",
      "388\n",
      "389\n",
      "390\n",
      "391\n",
      "392\n",
      "393\n",
      "394\n",
      "395\n",
      "396\n",
      "397\n",
      "398\n",
      "399\n",
      "400\n",
      "401\n",
      "402\n",
      "403\n",
      "404\n",
      "405\n",
      "406\n",
      "407\n",
      "408\n",
      "409\n",
      "410\n",
      "411\n",
      "412\n",
      "413\n",
      "414\n",
      "415\n",
      "416\n",
      "417\n",
      "418\n",
      "419\n",
      "420\n",
      "421\n",
      "422\n",
      "423\n",
      "424\n",
      "425\n",
      "426\n",
      "427\n",
      "428\n",
      "429\n",
      "430\n",
      "431\n",
      "432\n",
      "433\n",
      "434\n",
      "435\n",
      "436\n",
      "437\n",
      "438\n",
      "439\n",
      "440\n",
      "441\n",
      "442\n",
      "443\n",
      "444\n",
      "445\n",
      "446\n",
      "447\n",
      "448\n",
      "449\n",
      "450\n",
      "451\n",
      "452\n",
      "453\n",
      "454\n",
      "455\n",
      "456\n",
      "457\n",
      "458\n",
      "459\n",
      "460\n",
      "461\n",
      "462\n",
      "463\n",
      "464\n",
      "465\n",
      "466\n",
      "467\n",
      "468\n",
      "469\n",
      "470\n",
      "471\n",
      "472\n",
      "473\n",
      "474\n",
      "475\n",
      "476\n",
      "477\n",
      "478\n",
      "479\n",
      "480\n",
      "481\n",
      "482\n",
      "483\n",
      "484\n",
      "485\n",
      "486\n",
      "487\n",
      "488\n",
      "489\n",
      "490\n",
      "491\n",
      "492\n",
      "493\n",
      "494\n",
      "495\n",
      "496\n",
      "497\n",
      "498\n",
      "499\n",
      "500\n",
      "501\n",
      "502\n",
      "503\n",
      "504\n",
      "505\n",
      "506\n",
      "507\n",
      "508\n",
      "509\n",
      "510\n",
      "511\n",
      "512\n",
      "513\n",
      "514\n",
      "515\n",
      "516\n",
      "517\n",
      "518\n",
      "519\n",
      "520\n",
      "521\n",
      "522\n",
      "523\n",
      "524\n",
      "525\n",
      "526\n",
      "527\n",
      "528\n",
      "529\n",
      "530\n",
      "531\n",
      "532\n",
      "533\n",
      "534\n",
      "535\n",
      "536\n",
      "537\n",
      "538\n",
      "539\n",
      "540\n",
      "541\n",
      "542\n",
      "543\n",
      "544\n",
      "545\n",
      "546\n",
      "547\n",
      "548\n",
      "549\n",
      "550\n",
      "551\n",
      "552\n",
      "553\n",
      "554\n",
      "555\n",
      "556\n",
      "557\n",
      "558\n",
      "559\n",
      "560\n",
      "561\n",
      "562\n",
      "563\n",
      "564\n",
      "565\n",
      "566\n",
      "567\n",
      "568\n",
      "569\n",
      "570\n",
      "571\n",
      "572\n",
      "573\n",
      "574\n",
      "575\n",
      "576\n",
      "577\n",
      "578\n",
      "579\n",
      "580\n",
      "581\n",
      "582\n",
      "583\n",
      "584\n",
      "585\n",
      "586\n",
      "587\n",
      "588\n",
      "589\n",
      "590\n",
      "591\n",
      "592\n",
      "593\n",
      "594\n",
      "595\n",
      "596\n",
      "597\n",
      "598\n",
      "599\n",
      "600\n",
      "601\n",
      "602\n",
      "603\n",
      "604\n",
      "605\n",
      "606\n",
      "607\n",
      "608\n",
      "609\n",
      "610\n",
      "611\n",
      "612\n",
      "613\n",
      "614\n",
      "615\n",
      "616\n",
      "617\n",
      "618\n",
      "619\n",
      "620\n",
      "621\n",
      "622\n",
      "623\n",
      "624\n",
      "625\n",
      "626\n",
      "627\n",
      "628\n",
      "629\n",
      "630\n",
      "631\n",
      "632\n",
      "633\n",
      "634\n",
      "635\n",
      "636\n",
      "637\n",
      "638\n",
      "639\n",
      "640\n",
      "641\n",
      "642\n",
      "643\n",
      "644\n",
      "645\n",
      "646\n",
      "647\n",
      "648\n",
      "649\n",
      "650\n",
      "651\n",
      "652\n",
      "653\n",
      "654\n",
      "655\n",
      "656\n",
      "657\n",
      "658\n",
      "659\n",
      "660\n",
      "661\n",
      "662\n",
      "663\n",
      "664\n",
      "665\n",
      "666\n",
      "667\n",
      "668\n",
      "669\n",
      "670\n",
      "671\n",
      "672\n",
      "673\n",
      "674\n",
      "675\n",
      "676\n",
      "677\n",
      "678\n",
      "679\n",
      "680\n",
      "681\n",
      "682\n",
      "683\n",
      "684\n",
      "685\n",
      "686\n",
      "687\n",
      "688\n",
      "689\n",
      "690\n",
      "691\n",
      "692\n",
      "693\n",
      "694\n",
      "695\n",
      "696\n",
      "697\n",
      "698\n",
      "699\n",
      "700\n",
      "701\n",
      "702\n",
      "703\n",
      "704\n",
      "705\n",
      "706\n",
      "707\n",
      "708\n",
      "709\n",
      "710\n",
      "711\n",
      "712\n",
      "713\n",
      "714\n",
      "715\n",
      "716\n",
      "717\n",
      "718\n",
      "719\n",
      "720\n",
      "721\n",
      "722\n",
      "723\n",
      "724\n",
      "725\n",
      "726\n",
      "727\n",
      "728\n",
      "729\n",
      "730\n",
      "731\n",
      "732\n",
      "733\n",
      "734\n",
      "735\n",
      "736\n",
      "737\n",
      "738\n",
      "739\n",
      "740\n",
      "741\n",
      "742\n",
      "743\n",
      "744\n",
      "745\n",
      "746\n",
      "747\n",
      "748\n",
      "749\n",
      "750\n",
      "751\n",
      "752\n",
      "753\n",
      "754\n",
      "755\n",
      "756\n",
      "757\n",
      "758\n",
      "759\n",
      "760\n",
      "761\n",
      "762\n",
      "763\n",
      "764\n",
      "765\n",
      "766\n",
      "767\n",
      "768\n",
      "769\n",
      "770\n",
      "771\n",
      "772\n",
      "773\n",
      "774\n",
      "775\n",
      "776\n",
      "777\n",
      "778\n",
      "779\n",
      "780\n",
      "781\n",
      "782\n",
      "783\n",
      "784\n",
      "785\n",
      "786\n",
      "787\n",
      "788\n",
      "789\n",
      "790\n",
      "791\n",
      "792\n",
      "793\n",
      "794\n",
      "795\n",
      "796\n",
      "797\n",
      "798\n",
      "799\n",
      "800\n",
      "801\n",
      "802\n",
      "803\n",
      "804\n",
      "805\n",
      "806\n",
      "807\n",
      "808\n",
      "809\n",
      "810\n",
      "811\n",
      "812\n",
      "813\n",
      "814\n",
      "815\n",
      "816\n",
      "817\n",
      "818\n",
      "819\n",
      "820\n",
      "821\n",
      "822\n",
      "823\n",
      "824\n",
      "825\n",
      "826\n",
      "827\n",
      "828\n",
      "829\n",
      "830\n",
      "831\n",
      "832\n",
      "833\n",
      "834\n",
      "835\n",
      "836\n",
      "837\n",
      "838\n",
      "839\n",
      "840\n",
      "841\n",
      "842\n",
      "843\n",
      "844\n",
      "845\n",
      "846\n",
      "847\n",
      "848\n",
      "849\n",
      "850\n",
      "851\n",
      "852\n",
      "853\n",
      "854\n",
      "855\n",
      "856\n",
      "857\n",
      "858\n",
      "859\n",
      "860\n",
      "861\n",
      "862\n",
      "863\n",
      "864\n",
      "865\n",
      "866\n",
      "867\n",
      "868\n",
      "869\n",
      "870\n",
      "871\n",
      "872\n",
      "873\n",
      "874\n",
      "875\n",
      "876\n",
      "877\n",
      "878\n",
      "879\n",
      "880\n",
      "881\n",
      "882\n",
      "883\n",
      "884\n",
      "885\n",
      "886\n",
      "887\n",
      "888\n",
      "889\n",
      "890\n",
      "891\n",
      "892\n",
      "893\n",
      "894\n",
      "895\n",
      "896\n",
      "897\n",
      "898\n",
      "899\n",
      "900\n",
      "901\n",
      "902\n",
      "903\n",
      "904\n",
      "905\n",
      "906\n",
      "907\n",
      "908\n",
      "909\n",
      "910\n",
      "911\n",
      "912\n",
      "913\n",
      "914\n",
      "915\n",
      "916\n",
      "917\n",
      "918\n",
      "919\n",
      "920\n",
      "921\n",
      "922\n",
      "923\n",
      "924\n",
      "925\n",
      "926\n",
      "927\n",
      "928\n",
      "929\n",
      "930\n",
      "931\n",
      "932\n",
      "933\n",
      "934\n",
      "935\n",
      "936\n",
      "937\n",
      "938\n",
      "939\n",
      "940\n",
      "941\n",
      "942\n",
      "943\n",
      "944\n",
      "945\n",
      "946\n",
      "947\n",
      "948\n",
      "949\n",
      "950\n",
      "951\n",
      "952\n",
      "953\n",
      "954\n",
      "955\n",
      "956\n",
      "957\n",
      "958\n",
      "959\n",
      "960\n",
      "961\n",
      "962\n",
      "963\n",
      "964\n",
      "965\n",
      "966\n",
      "967\n",
      "968\n",
      "969\n",
      "970\n",
      "971\n",
      "972\n",
      "973\n",
      "974\n",
      "975\n",
      "976\n",
      "977\n",
      "978\n",
      "979\n",
      "980\n",
      "981\n",
      "982\n",
      "983\n",
      "984\n",
      "985\n",
      "986\n",
      "987\n",
      "988\n",
      "989\n",
      "990\n",
      "991\n",
      "992\n",
      "993\n",
      "994\n",
      "995\n",
      "996\n",
      "997\n",
      "998\n",
      "999\n",
      "1000\n"
     ]
    }
   ],
   "source": [
    "#take about 15mins\n",
    "top1_id = []\n",
    "top1_fre = []\n",
    "top2_id = []\n",
    "top2_fre = []\n",
    "a = 0\n",
    "for id in np.arange(1000)+1:\n",
    "    lens =len(data3.set_index('玩家id').loc[id].index)\n",
    "    for i in np.arange(lens)+1:\n",
    "        top1 = data3.set_index('玩家id').loc[id].iloc[:i,:]['计费点id'].mode().values[0]\n",
    "        freq1 = data3.set_index('玩家id').loc[id].iloc[:i,:]['计费点id'].value_counts(normalize=True).values[0]\n",
    "        tops_num = len(data3.set_index('玩家id').loc[id].iloc[:i,:]['计费点id'].value_counts(normalize=True).index)\n",
    "        \n",
    "        top1_id.append(top1)\n",
    "        top1_fre.append(freq1)\n",
    "        \n",
    "\n",
    "        if tops_num>=2:\n",
    "            \n",
    "            top2 = data3.set_index('玩家id').loc[id].iloc[:i,:]['计费点id'].value_counts(normalize=True).index[1]\n",
    "            freq2 = data3.set_index('玩家id').loc[id].iloc[:i,:]['计费点id'].value_counts(normalize=True).values[1]\n",
    "            top2_id.append(top2)\n",
    "            top2_fre.append(freq2)\n",
    "        else:\n",
    "            top2_id.append(a)\n",
    "            top2_fre.append(a)\n",
    "    print(id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "data3['最高频计费点'] = np.array(top1_id)\n",
    "data3['最高频计费点fre'] = np.array(top1_fre)\n",
    "data3['第二高频计费点']=np.array(top2_id)\n",
    "data3['第二高频计费点fre']=np.array(top2_fre)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "data3.loc[data3['首次付费']==1,'最高频计费点']=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "data3.to_excel('付费分析all.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "data3 = pd.read_excel('付费分析all.xlsx')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 建模"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import OneHotEncoder\n",
    "from sklearn.metrics import classification_report\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.naive_bayes import MultinomialNB\n",
    "from sklearn.svm import SVC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>玩家id</th>\n",
       "      <th>注册时间</th>\n",
       "      <th>付费时间</th>\n",
       "      <th>付费额度</th>\n",
       "      <th>计费点id</th>\n",
       "      <th>首天付费</th>\n",
       "      <th>首次付费</th>\n",
       "      <th>游玩天数</th>\n",
       "      <th>月份</th>\n",
       "      <th>...</th>\n",
       "      <th>是否是周末</th>\n",
       "      <th>距上次付费的天数</th>\n",
       "      <th>累计消费额</th>\n",
       "      <th>累计订单数</th>\n",
       "      <th>均单价</th>\n",
       "      <th>上次的计费点</th>\n",
       "      <th>最高频计费点</th>\n",
       "      <th>最高频计费点fre</th>\n",
       "      <th>第二高频计费点</th>\n",
       "      <th>第二高频计费点fre</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>1.99</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>9.99</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1.99</td>\n",
       "      <td>1</td>\n",
       "      <td>1.99</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>1</td>\n",
       "      <td>0.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>29.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>11.98</td>\n",
       "      <td>2</td>\n",
       "      <td>5.99</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>1</td>\n",
       "      <td>0.333333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>19.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>41.97</td>\n",
       "      <td>3</td>\n",
       "      <td>13.99</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0.750000</td>\n",
       "      <td>1</td>\n",
       "      <td>0.250000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>24.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>61.96</td>\n",
       "      <td>4</td>\n",
       "      <td>15.49</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0.800000</td>\n",
       "      <td>1</td>\n",
       "      <td>0.200000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   Unnamed: 0  玩家id       注册时间       付费时间   付费额度  计费点id  首天付费  首次付费  游玩天数  月份  \\\n",
       "0           0     1 2019-11-15 2019-11-15   1.99      3     1     1     0  11   \n",
       "1           1     1 2019-11-15 2019-11-15   9.99      1     1     0     0  11   \n",
       "2           2     1 2019-11-15 2019-11-16  29.99      3     0     0     1  11   \n",
       "3           3     1 2019-11-15 2019-11-16  19.99      3     0     0     1  11   \n",
       "4           4     1 2019-11-15 2019-11-16  24.99      3     0     0     1  11   \n",
       "\n",
       "   ...  是否是周末  距上次付费的天数  累计消费额  累计订单数    均单价  上次的计费点  最高频计费点  最高频计费点fre  \\\n",
       "0  ...      1         0   0.00      0   0.00       0       0   1.000000   \n",
       "1  ...      1         0   1.99      1   1.99       3       1   0.500000   \n",
       "2  ...      1         1  11.98      2   5.99       1       3   0.666667   \n",
       "3  ...      1         0  41.97      3  13.99       3       3   0.750000   \n",
       "4  ...      1         0  61.96      4  15.49       3       3   0.800000   \n",
       "\n",
       "   第二高频计费点  第二高频计费点fre  \n",
       "0        0    0.000000  \n",
       "1        1    0.500000  \n",
       "2        1    0.333333  \n",
       "3        1    0.250000  \n",
       "4        1    0.200000  \n",
       "\n",
       "[5 rows x 21 columns]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data3.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        3\n",
       "1        1\n",
       "2        3\n",
       "3        3\n",
       "4        3\n",
       "        ..\n",
       "31941    3\n",
       "31942    3\n",
       "31943    3\n",
       "31944    3\n",
       "31945    3\n",
       "Name: 计费点id, Length: 31946, dtype: int64"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data3.iloc[:,5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = data3.iloc[:,6:]\n",
    "y = data3.iloc[:,5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>玩家id</th>\n",
       "      <th>注册时间</th>\n",
       "      <th>付费时间</th>\n",
       "      <th>付费额度</th>\n",
       "      <th>计费点id</th>\n",
       "      <th>首天付费</th>\n",
       "      <th>首次付费</th>\n",
       "      <th>游玩天数</th>\n",
       "      <th>月份</th>\n",
       "      <th>星期</th>\n",
       "      <th>是否是周末</th>\n",
       "      <th>距上次付费的天数</th>\n",
       "      <th>累计消费额</th>\n",
       "      <th>累计订单数</th>\n",
       "      <th>均单价</th>\n",
       "      <th>上次的计费点</th>\n",
       "      <th>最高频计费点</th>\n",
       "      <th>最高频计费点fre</th>\n",
       "      <th>第二高频计费点</th>\n",
       "      <th>第二高频计费点fre</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>1.99</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>9.99</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1.99</td>\n",
       "      <td>1</td>\n",
       "      <td>1.99</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>1</td>\n",
       "      <td>0.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>29.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>11.98</td>\n",
       "      <td>2</td>\n",
       "      <td>5.99</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>1</td>\n",
       "      <td>0.333333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>19.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>41.97</td>\n",
       "      <td>3</td>\n",
       "      <td>13.99</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0.750000</td>\n",
       "      <td>1</td>\n",
       "      <td>0.250000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>2019-11-16</td>\n",
       "      <td>24.99</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>61.96</td>\n",
       "      <td>4</td>\n",
       "      <td>15.49</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0.800000</td>\n",
       "      <td>1</td>\n",
       "      <td>0.200000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   玩家id       注册时间       付费时间   付费额度  计费点id  首天付费  首次付费  游玩天数  月份  星期  是否是周末  \\\n",
       "0     1 2019-11-15 2019-11-15   1.99      3     1     1     0  11   4      1   \n",
       "1     1 2019-11-15 2019-11-15   9.99      1     1     0     0  11   4      1   \n",
       "2     1 2019-11-15 2019-11-16  29.99      3     0     0     1  11   5      1   \n",
       "3     1 2019-11-15 2019-11-16  19.99      3     0     0     1  11   5      1   \n",
       "4     1 2019-11-15 2019-11-16  24.99      3     0     0     1  11   5      1   \n",
       "\n",
       "   距上次付费的天数  累计消费额  累计订单数    均单价  上次的计费点  最高频计费点  最高频计费点fre  第二高频计费点  \\\n",
       "0         0   0.00      0   0.00       0       0   1.000000        0   \n",
       "1         0   1.99      1   1.99       3       1   0.500000        1   \n",
       "2         1  11.98      2   5.99       1       3   0.666667        1   \n",
       "3         0  41.97      3  13.99       3       3   0.750000        1   \n",
       "4         0  61.96      4  15.49       3       3   0.800000        1   \n",
       "\n",
       "   第二高频计费点fre  \n",
       "0    0.000000  \n",
       "1    0.500000  \n",
       "2    0.333333  \n",
       "3    0.250000  \n",
       "4    0.200000  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "xtrain,xtest,ytrain,ytest = train_test_split(x,y,random_state = 200)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## GBDT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           1       0.66      0.56      0.61      1905\n",
      "           2       0.74      0.79      0.76      2301\n",
      "           3       0.75      0.77      0.76      3781\n",
      "\n",
      "    accuracy                           0.73      7987\n",
      "   macro avg       0.72      0.71      0.71      7987\n",
      "weighted avg       0.72      0.73      0.72      7987\n",
      "\n"
     ]
    }
   ],
   "source": [
    "GBDT = GradientBoostingClassifier().fit(xtrain,ytrain)\n",
    "print(classification_report(ytest,GBDT.predict(xtest)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7274320771253286"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "GBDT.score(xtest,ytest)\n",
    "#准确率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1069,  190,  646],\n",
       "       [ 147, 1811,  343],\n",
       "       [ 402,  449, 2930]], dtype=int64)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import confusion_matrix\n",
    "confusion_matrix(ytest,GBDT.predict(xtest))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('首天付费', 0.0032944770515392716),\n",
       " ('首次付费', 0.011839036833502544),\n",
       " ('游玩天数', 0.022242513123002575),\n",
       " ('月份', 0.0027168027605136624),\n",
       " ('星期', 0.0008425132630304885),\n",
       " ('是否是周末', 3.868530887078026e-05),\n",
       " ('距上次付费的天数', 0.009697996241527042),\n",
       " ('累计消费额', 0.015241667025468116),\n",
       " ('累计订单数', 0.018298955256360574),\n",
       " ('均单价', 0.10881521491126597),\n",
       " ('上次的计费点', 0.09858099612650778),\n",
       " ('最高频计费点', 0.5072543665946766),\n",
       " ('最高频计费点fre', 0.1027977952517847),\n",
       " ('第二高频计费点', 0.04045872140906205),\n",
       " ('第二高频计费点fre', 0.057880258842887944)]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[*zip(xtrain.columns,GBDT.feature_importances_)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## DT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "DT = DecisionTreeClassifier()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=10, estimator=DecisionTreeClassifier(random_state=25),\n",
       "             param_grid={'criterion': ('gini', 'entropy'),\n",
       "                         'max_depth': [1, 2, 3, 4, 5, 6, 7, 8, 9],\n",
       "                         'min_impurity_decrease': [0.0, 0.02631578947368421,\n",
       "                                                   0.05263157894736842,\n",
       "                                                   0.07894736842105263,\n",
       "                                                   0.10526315789473684,\n",
       "                                                   0.13157894736842105,\n",
       "                                                   0.15789473684210525,\n",
       "                                                   0.18421052631578946,\n",
       "                                                   0.21052631578947367,\n",
       "                                                   0.23684210526315788,\n",
       "                                                   0.2631578947368421,\n",
       "                                                   0.2894736842105263,\n",
       "                                                   0.3157894736842105,\n",
       "                                                   0.3421052631578947,\n",
       "                                                   0.3684210526315789,\n",
       "                                                   0.39473684210526316,\n",
       "                                                   0.42105263157894735,\n",
       "                                                   0.4473684210526315,\n",
       "                                                   0.47368421052631576, 0.5],\n",
       "                         'min_samples_leaf': [1, 6, 11, 16, 21, 26, 31, 36, 41,\n",
       "                                              46],\n",
       "                         'splitter': ('best', 'random')})"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.model_selection import GridSearchCV\n",
    "#take about 20min\n",
    "parameters={'criterion':(\"gini\",\"entropy\")\n",
    "              ,\"max_depth\":[*range(1,30)]\n",
    "              ,'min_samples_leaf':[*range(1,50,5)]\n",
    "              ,'min_impurity_decrease':[*np.linspace(0,0.5,20)] #信息增益最小值\n",
    "             }\n",
    "clf = DecisionTreeClassifier(random_state=25)\n",
    "GS = GridSearchCV(clf,parameters,cv=10)\n",
    "GS.fit(xtrain,ytrain)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'criterion': 'gini',\n",
       " 'max_depth': 7,\n",
       " 'min_impurity_decrease': 0.0,\n",
       " 'min_samples_leaf': 6,\n",
       " 'splitter': 'random'}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "GS.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7132188128439536"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "GS.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           1       0.61      0.61      0.61      1905\n",
      "           2       0.66      0.83      0.74      2301\n",
      "           3       0.78      0.66      0.72      3781\n",
      "\n",
      "    accuracy                           0.70      7987\n",
      "   macro avg       0.68      0.70      0.69      7987\n",
      "weighted avg       0.71      0.70      0.70      7987\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[1155,  282,  468],\n",
       "       [ 165, 1913,  223],\n",
       "       [ 570,  706, 2505]], dtype=int64)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "DT = DecisionTreeClassifier(random_state=25\n",
    "                             ,criterion='gini'\n",
    "                             ,max_depth=4\n",
    "                             ,min_impurity_decrease=0\n",
    "                             ,min_samples_leaf=6\n",
    "                            ).fit(xtrain,ytrain)\n",
    "print(classification_report(ytest,DT.predict(xtest)))\n",
    "confusion_matrix(ytest,DT.predict(xtest))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('首天付费', 0.0),\n",
       " ('首次付费', 0.0),\n",
       " ('游玩天数', 0.006218372699511396),\n",
       " ('月份', 0.0),\n",
       " ('星期', 0.0),\n",
       " ('是否是周末', 0.0),\n",
       " ('距上次付费的天数', 0.0005643641335007192),\n",
       " ('累计消费额', 0.008669877270456948),\n",
       " ('累计订单数', 0.0),\n",
       " ('均单价', 0.02530420268454669),\n",
       " ('上次的计费点', 0.005715405496536383),\n",
       " ('最高频计费点', 0.7519053579944334),\n",
       " ('最高频计费点fre', 0.17766790672123278),\n",
       " ('第二高频计费点', 0.02395451299978168),\n",
       " ('第二高频计费点fre', 0.0)]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[*zip(xtrain.columns,DT.feature_importances_)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\r\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\r\n",
       "<!-- Generated by graphviz version 2.38.0 (20140413.2041)\r\n",
       " -->\r\n",
       "<!-- Title: Tree Pages: 1 -->\r\n",
       "<svg width=\"2818pt\" height=\"552pt\"\r\n",
       " viewBox=\"0.00 0.00 2817.50 552.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\r\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 548)\">\r\n",
       "<title>Tree</title>\r\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-548 2813.5,-548 2813.5,4 -4,4\"/>\r\n",
       "<!-- 0 -->\r\n",
       "<g id=\"node1\" class=\"node\"><title>0</title>\r\n",
       "<path fill=\"#e0cef9\" stroke=\"black\" d=\"M1529.5,-544C1529.5,-544 1358.5,-544 1358.5,-544 1352.5,-544 1346.5,-538 1346.5,-532 1346.5,-532 1346.5,-473 1346.5,-473 1346.5,-467 1352.5,-461 1358.5,-461 1358.5,-461 1529.5,-461 1529.5,-461 1535.5,-461 1541.5,-467 1541.5,-473 1541.5,-473 1541.5,-532 1541.5,-532 1541.5,-538 1535.5,-544 1529.5,-544\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"1444\" y=\"-528.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">最高频计费点 &lt;= 2.5</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1444\" y=\"-513.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.639</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1444\" y=\"-498.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 23959</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1444\" y=\"-483.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [5810, 6972, 11177]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1444\" y=\"-468.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 3</text>\r\n",
       "</g>\r\n",
       "<!-- 1 -->\r\n",
       "<g id=\"node2\" class=\"node\"><title>1</title>\r\n",
       "<path fill=\"#d9fae7\" stroke=\"black\" d=\"M1170.5,-425C1170.5,-425 1007.5,-425 1007.5,-425 1001.5,-425 995.5,-419 995.5,-413 995.5,-413 995.5,-354 995.5,-354 995.5,-348 1001.5,-342 1007.5,-342 1007.5,-342 1170.5,-342 1170.5,-342 1176.5,-342 1182.5,-348 1182.5,-354 1182.5,-354 1182.5,-413 1182.5,-413 1182.5,-419 1176.5,-425 1170.5,-425\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"1089\" y=\"-409.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">最高频计费点 &lt;= 1.5</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1089\" y=\"-394.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.647</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1089\" y=\"-379.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 14121</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1089\" y=\"-364.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [4353, 6244, 3524]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1089\" y=\"-349.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 2</text>\r\n",
       "</g>\r\n",
       "<!-- 0&#45;&gt;1 -->\r\n",
       "<g id=\"edge1\" class=\"edge\"><title>0&#45;&gt;1</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M1346.05,-469.22C1298.24,-453.462 1240.72,-434.504 1192.22,-418.52\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"1193.24,-415.171 1182.65,-415.365 1191.05,-421.819 1193.24,-415.171\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"1193.91\" y=\"-433.988\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">True</text>\r\n",
       "</g>\r\n",
       "<!-- 16 -->\r\n",
       "<g id=\"node17\" class=\"node\"><title>16</title>\r\n",
       "<path fill=\"#a26dec\" stroke=\"black\" d=\"M1880.5,-425C1880.5,-425 1725.5,-425 1725.5,-425 1719.5,-425 1713.5,-419 1713.5,-413 1713.5,-413 1713.5,-354 1713.5,-354 1713.5,-348 1719.5,-342 1725.5,-342 1725.5,-342 1880.5,-342 1880.5,-342 1886.5,-342 1892.5,-348 1892.5,-354 1892.5,-354 1892.5,-413 1892.5,-413 1892.5,-419 1886.5,-425 1880.5,-425\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"1803\" y=\"-409.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">最高频计费点fre &lt;= 0.668</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1803\" y=\"-394.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.367</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1803\" y=\"-379.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 9838</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1803\" y=\"-364.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [1457, 728, 7653]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1803\" y=\"-349.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 3</text>\r\n",
       "</g>\r\n",
       "<!-- 0&#45;&gt;16 -->\r\n",
       "<g id=\"edge16\" class=\"edge\"><title>0&#45;&gt;16</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M1541.62,-469.684C1591.95,-453.282 1653.2,-433.32 1703.76,-416.843\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"1704.97,-420.129 1713.39,-413.703 1702.8,-413.474 1704.97,-420.129\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"1702.06\" y=\"-432.288\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">False</text>\r\n",
       "</g>\r\n",
       "<!-- 2 -->\r\n",
       "<g id=\"node3\" class=\"node\"><title>2</title>\r\n",
       "<path fill=\"#f5ccaf\" stroke=\"black\" d=\"M606.5,-306C606.5,-306 451.5,-306 451.5,-306 445.5,-306 439.5,-300 439.5,-294 439.5,-294 439.5,-235 439.5,-235 439.5,-229 445.5,-223 451.5,-223 451.5,-223 606.5,-223 606.5,-223 612.5,-223 618.5,-229 618.5,-235 618.5,-235 618.5,-294 618.5,-294 618.5,-300 612.5,-306 606.5,-306\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"529\" y=\"-290.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">均单价 &lt;= 0.495</text>\r\n",
       "<text text-anchor=\"middle\" x=\"529\" y=\"-275.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.558</text>\r\n",
       "<text text-anchor=\"middle\" x=\"529\" y=\"-260.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 5821</text>\r\n",
       "<text text-anchor=\"middle\" x=\"529\" y=\"-245.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [3387, 691, 1743]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"529\" y=\"-230.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 1</text>\r\n",
       "</g>\r\n",
       "<!-- 1&#45;&gt;2 -->\r\n",
       "<g id=\"edge2\" class=\"edge\"><title>1&#45;&gt;2</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M995.269,-362.917C893.769,-341.711 732.504,-308.018 628.723,-286.335\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"629.368,-282.894 618.863,-284.275 627.936,-289.746 629.368,-282.894\"/>\r\n",
       "</g>\r\n",
       "<!-- 9 -->\r\n",
       "<g id=\"node10\" class=\"node\"><title>9</title>\r\n",
       "<path fill=\"#8cf0b6\" stroke=\"black\" d=\"M1166.5,-306C1166.5,-306 1011.5,-306 1011.5,-306 1005.5,-306 999.5,-300 999.5,-294 999.5,-294 999.5,-235 999.5,-235 999.5,-229 1005.5,-223 1011.5,-223 1011.5,-223 1166.5,-223 1166.5,-223 1172.5,-223 1178.5,-229 1178.5,-235 1178.5,-235 1178.5,-294 1178.5,-294 1178.5,-300 1172.5,-306 1166.5,-306\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"1089\" y=\"-290.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">最高频计费点fre &lt;= 0.734</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1089\" y=\"-275.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.493</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1089\" y=\"-260.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 8300</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1089\" y=\"-245.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [966, 5553, 1781]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1089\" y=\"-230.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 2</text>\r\n",
       "</g>\r\n",
       "<!-- 1&#45;&gt;9 -->\r\n",
       "<g id=\"edge9\" class=\"edge\"><title>1&#45;&gt;9</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M1089,-341.907C1089,-333.649 1089,-324.864 1089,-316.302\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"1092.5,-316.021 1089,-306.021 1085.5,-316.021 1092.5,-316.021\"/>\r\n",
       "</g>\r\n",
       "<!-- 3 -->\r\n",
       "<g id=\"node4\" class=\"node\"><title>3</title>\r\n",
       "<path fill=\"#e8fcf0\" stroke=\"black\" d=\"M314,-187C314,-187 176,-187 176,-187 170,-187 164,-181 164,-175 164,-175 164,-116 164,-116 164,-110 170,-104 176,-104 176,-104 314,-104 314,-104 320,-104 326,-110 326,-116 326,-116 326,-175 326,-175 326,-181 320,-187 314,-187\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"245\" y=\"-171.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">游玩天数 &lt;= 0.5</text>\r\n",
       "<text text-anchor=\"middle\" x=\"245\" y=\"-156.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.66</text>\r\n",
       "<text text-anchor=\"middle\" x=\"245\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 767</text>\r\n",
       "<text text-anchor=\"middle\" x=\"245\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [219, 305, 243]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"245\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 2</text>\r\n",
       "</g>\r\n",
       "<!-- 2&#45;&gt;3 -->\r\n",
       "<g id=\"edge3\" class=\"edge\"><title>2&#45;&gt;3</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M439.141,-226.48C406.158,-212.892 368.816,-197.509 335.755,-183.889\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"336.677,-180.483 326.098,-179.91 334.011,-186.955 336.677,-180.483\"/>\r\n",
       "</g>\r\n",
       "<!-- 6 -->\r\n",
       "<g id=\"node7\" class=\"node\"><title>6</title>\r\n",
       "<path fill=\"#f3c4a2\" stroke=\"black\" d=\"M606.5,-187C606.5,-187 451.5,-187 451.5,-187 445.5,-187 439.5,-181 439.5,-175 439.5,-175 439.5,-116 439.5,-116 439.5,-110 445.5,-104 451.5,-104 451.5,-104 606.5,-104 606.5,-104 612.5,-104 618.5,-110 618.5,-116 618.5,-116 618.5,-175 618.5,-175 618.5,-181 612.5,-187 606.5,-187\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"529\" y=\"-171.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">最高频计费点fre &lt;= 0.713</text>\r\n",
       "<text text-anchor=\"middle\" x=\"529\" y=\"-156.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.513</text>\r\n",
       "<text text-anchor=\"middle\" x=\"529\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 5054</text>\r\n",
       "<text text-anchor=\"middle\" x=\"529\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [3168, 386, 1500]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"529\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 1</text>\r\n",
       "</g>\r\n",
       "<!-- 2&#45;&gt;6 -->\r\n",
       "<g id=\"edge6\" class=\"edge\"><title>2&#45;&gt;6</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M529,-222.907C529,-214.649 529,-205.864 529,-197.302\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"532.5,-197.021 529,-187.021 525.5,-197.021 532.5,-197.021\"/>\r\n",
       "</g>\r\n",
       "<!-- 4 -->\r\n",
       "<g id=\"node5\" class=\"node\"><title>4</title>\r\n",
       "<path fill=\"#a3f3c5\" stroke=\"black\" d=\"M134,-68C134,-68 12,-68 12,-68 6,-68 0,-62 0,-56 0,-56 0,-12 0,-12 0,-6 6,-0 12,-0 12,-0 134,-0 134,-0 140,-0 146,-6 146,-12 146,-12 146,-56 146,-56 146,-62 140,-68 134,-68\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"73\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.56</text>\r\n",
       "<text text-anchor=\"middle\" x=\"73\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 336</text>\r\n",
       "<text text-anchor=\"middle\" x=\"73\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [53, 200, 83]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"73\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 2</text>\r\n",
       "</g>\r\n",
       "<!-- 3&#45;&gt;4 -->\r\n",
       "<g id=\"edge4\" class=\"edge\"><title>3&#45;&gt;4</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M180.954,-103.726C165.544,-93.9161 149.127,-83.4644 133.867,-73.7496\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"135.348,-70.5431 125.032,-68.1252 131.588,-76.4481 135.348,-70.5431\"/>\r\n",
       "</g>\r\n",
       "<!-- 5 -->\r\n",
       "<g id=\"node6\" class=\"node\"><title>5</title>\r\n",
       "<path fill=\"#fefcfb\" stroke=\"black\" d=\"M314,-68C314,-68 176,-68 176,-68 170,-68 164,-62 164,-56 164,-56 164,-12 164,-12 164,-6 170,-0 176,-0 176,-0 314,-0 314,-0 320,-0 326,-6 326,-12 326,-12 326,-56 326,-56 326,-62 320,-68 314,-68\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"245\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.654</text>\r\n",
       "<text text-anchor=\"middle\" x=\"245\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 431</text>\r\n",
       "<text text-anchor=\"middle\" x=\"245\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [166, 105, 160]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"245\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 1</text>\r\n",
       "</g>\r\n",
       "<!-- 3&#45;&gt;5 -->\r\n",
       "<g id=\"edge5\" class=\"edge\"><title>3&#45;&gt;5</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M245,-103.726C245,-95.5175 245,-86.8595 245,-78.56\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"248.5,-78.2996 245,-68.2996 241.5,-78.2996 248.5,-78.2996\"/>\r\n",
       "</g>\r\n",
       "<!-- 7 -->\r\n",
       "<g id=\"node8\" class=\"node\"><title>7</title>\r\n",
       "<path fill=\"#fae7da\" stroke=\"black\" d=\"M511.5,-68C511.5,-68 356.5,-68 356.5,-68 350.5,-68 344.5,-62 344.5,-56 344.5,-56 344.5,-12 344.5,-12 344.5,-6 350.5,-0 356.5,-0 356.5,-0 511.5,-0 511.5,-0 517.5,-0 523.5,-6 523.5,-12 523.5,-12 523.5,-56 523.5,-56 523.5,-62 517.5,-68 511.5,-68\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"434\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.582</text>\r\n",
       "<text text-anchor=\"middle\" x=\"434\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 3231</text>\r\n",
       "<text text-anchor=\"middle\" x=\"434\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [1630, 342, 1259]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"434\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 1</text>\r\n",
       "</g>\r\n",
       "<!-- 6&#45;&gt;7 -->\r\n",
       "<g id=\"edge7\" class=\"edge\"><title>6&#45;&gt;7</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M493.626,-103.726C485.792,-94.6966 477.486,-85.1235 469.642,-76.0816\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"472.087,-73.5595 462.89,-68.2996 466.8,-78.1467 472.087,-73.5595\"/>\r\n",
       "</g>\r\n",
       "<!-- 8 -->\r\n",
       "<g id=\"node9\" class=\"node\"><title>8</title>\r\n",
       "<path fill=\"#ea985d\" stroke=\"black\" d=\"M692,-68C692,-68 554,-68 554,-68 548,-68 542,-62 542,-56 542,-56 542,-12 542,-12 542,-6 548,-0 554,-0 554,-0 692,-0 692,-0 698,-0 704,-6 704,-12 704,-12 704,-56 704,-56 704,-62 698,-68 692,-68\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"623\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.27</text>\r\n",
       "<text text-anchor=\"middle\" x=\"623\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 1823</text>\r\n",
       "<text text-anchor=\"middle\" x=\"623\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [1538, 44, 241]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"623\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 1</text>\r\n",
       "</g>\r\n",
       "<!-- 6&#45;&gt;8 -->\r\n",
       "<g id=\"edge8\" class=\"edge\"><title>6&#45;&gt;8</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M564.002,-103.726C571.754,-94.6966 579.972,-85.1235 587.734,-76.0816\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"590.556,-78.1671 594.414,-68.2996 585.245,-73.6075 590.556,-78.1671\"/>\r\n",
       "</g>\r\n",
       "<!-- 10 -->\r\n",
       "<g id=\"node11\" class=\"node\"><title>10</title>\r\n",
       "<path fill=\"#dcfae9\" stroke=\"black\" d=\"M1069.5,-187C1069.5,-187 914.5,-187 914.5,-187 908.5,-187 902.5,-181 902.5,-175 902.5,-175 902.5,-116 902.5,-116 902.5,-110 908.5,-104 914.5,-104 914.5,-104 1069.5,-104 1069.5,-104 1075.5,-104 1081.5,-110 1081.5,-116 1081.5,-116 1081.5,-175 1081.5,-175 1081.5,-181 1075.5,-187 1069.5,-187\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"992\" y=\"-171.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">第二高频计费点 &lt;= 1.5</text>\r\n",
       "<text text-anchor=\"middle\" x=\"992\" y=\"-156.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.627</text>\r\n",
       "<text text-anchor=\"middle\" x=\"992\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 4114</text>\r\n",
       "<text text-anchor=\"middle\" x=\"992\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [764, 1914, 1436]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"992\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 2</text>\r\n",
       "</g>\r\n",
       "<!-- 9&#45;&gt;10 -->\r\n",
       "<g id=\"edge10\" class=\"edge\"><title>9&#45;&gt;10</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M1055.35,-222.907C1047.82,-213.832 1039.77,-204.121 1032.02,-194.769\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"1034.67,-192.485 1025.59,-187.021 1029.28,-196.953 1034.67,-192.485\"/>\r\n",
       "</g>\r\n",
       "<!-- 13 -->\r\n",
       "<g id=\"node14\" class=\"node\"><title>13</title>\r\n",
       "<path fill=\"#55e993\" stroke=\"black\" d=\"M1258.5,-187C1258.5,-187 1111.5,-187 1111.5,-187 1105.5,-187 1099.5,-181 1099.5,-175 1099.5,-175 1099.5,-116 1099.5,-116 1099.5,-110 1105.5,-104 1111.5,-104 1111.5,-104 1258.5,-104 1258.5,-104 1264.5,-104 1270.5,-110 1270.5,-116 1270.5,-116 1270.5,-175 1270.5,-175 1270.5,-181 1264.5,-187 1258.5,-187\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"1185\" y=\"-171.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">最高频计费点fre &lt;= 0.859</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1185\" y=\"-156.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.235</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1185\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 4186</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1185\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [202, 3639, 345]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1185\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 2</text>\r\n",
       "</g>\r\n",
       "<!-- 9&#45;&gt;13 -->\r\n",
       "<g id=\"edge13\" class=\"edge\"><title>9&#45;&gt;13</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M1122.31,-222.907C1129.75,-213.832 1137.72,-204.121 1145.39,-194.769\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"1148.11,-196.972 1151.75,-187.021 1142.7,-192.531 1148.11,-196.972\"/>\r\n",
       "</g>\r\n",
       "<!-- 11 -->\r\n",
       "<g id=\"node12\" class=\"node\"><title>11</title>\r\n",
       "<path fill=\"#e7fcf0\" stroke=\"black\" d=\"M872,-68C872,-68 734,-68 734,-68 728,-68 722,-62 722,-56 722,-56 722,-12 722,-12 722,-6 728,-0 734,-0 734,-0 872,-0 872,-0 878,-0 884,-6 884,-12 884,-12 884,-56 884,-56 884,-62 878,-68 872,-68\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"803\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.627</text>\r\n",
       "<text text-anchor=\"middle\" x=\"803\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 767</text>\r\n",
       "<text text-anchor=\"middle\" x=\"803\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [287, 344, 136]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"803\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 2</text>\r\n",
       "</g>\r\n",
       "<!-- 10&#45;&gt;11 -->\r\n",
       "<g id=\"edge11\" class=\"edge\"><title>10&#45;&gt;11</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M921.892,-103.882C904.537,-93.8269 886.003,-83.0892 868.859,-73.1563\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"870.441,-70.028 860.034,-68.0433 866.932,-76.0849 870.441,-70.028\"/>\r\n",
       "</g>\r\n",
       "<!-- 12 -->\r\n",
       "<g id=\"node13\" class=\"node\"><title>12</title>\r\n",
       "<path fill=\"#e5fcee\" stroke=\"black\" d=\"M1069.5,-68C1069.5,-68 914.5,-68 914.5,-68 908.5,-68 902.5,-62 902.5,-56 902.5,-56 902.5,-12 902.5,-12 902.5,-6 908.5,-0 914.5,-0 914.5,-0 1069.5,-0 1069.5,-0 1075.5,-0 1081.5,-6 1081.5,-12 1081.5,-12 1081.5,-56 1081.5,-56 1081.5,-62 1075.5,-68 1069.5,-68\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"992\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.609</text>\r\n",
       "<text text-anchor=\"middle\" x=\"992\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 3347</text>\r\n",
       "<text text-anchor=\"middle\" x=\"992\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [477, 1570, 1300]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"992\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 2</text>\r\n",
       "</g>\r\n",
       "<!-- 10&#45;&gt;12 -->\r\n",
       "<g id=\"edge12\" class=\"edge\"><title>10&#45;&gt;12</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M992,-103.726C992,-95.5175 992,-86.8595 992,-78.56\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"995.5,-78.2996 992,-68.2996 988.5,-78.2996 995.5,-78.2996\"/>\r\n",
       "</g>\r\n",
       "<!-- 14 -->\r\n",
       "<g id=\"node15\" class=\"node\"><title>14</title>\r\n",
       "<path fill=\"#71eca5\" stroke=\"black\" d=\"M1258,-68C1258,-68 1112,-68 1112,-68 1106,-68 1100,-62 1100,-56 1100,-56 1100,-12 1100,-12 1100,-6 1106,-0 1112,-0 1112,-0 1258,-0 1258,-0 1264,-0 1270,-6 1270,-12 1270,-12 1270,-56 1270,-56 1270,-62 1264,-68 1258,-68\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"1185\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.391</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1185\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 1775</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1185\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [157, 1350, 268]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1185\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 2</text>\r\n",
       "</g>\r\n",
       "<!-- 13&#45;&gt;14 -->\r\n",
       "<g id=\"edge14\" class=\"edge\"><title>13&#45;&gt;14</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M1185,-103.726C1185,-95.5175 1185,-86.8595 1185,-78.56\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"1188.5,-78.2996 1185,-68.2996 1181.5,-78.2996 1188.5,-78.2996\"/>\r\n",
       "</g>\r\n",
       "<!-- 15 -->\r\n",
       "<g id=\"node16\" class=\"node\"><title>15</title>\r\n",
       "<path fill=\"#43e688\" stroke=\"black\" d=\"M1430,-68C1430,-68 1300,-68 1300,-68 1294,-68 1288,-62 1288,-56 1288,-56 1288,-12 1288,-12 1288,-6 1294,-0 1300,-0 1300,-0 1430,-0 1430,-0 1436,-0 1442,-6 1442,-12 1442,-12 1442,-56 1442,-56 1442,-62 1436,-68 1430,-68\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"1365\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.097</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1365\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 2411</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1365\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [45, 2289, 77]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1365\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 2</text>\r\n",
       "</g>\r\n",
       "<!-- 13&#45;&gt;15 -->\r\n",
       "<g id=\"edge15\" class=\"edge\"><title>13&#45;&gt;15</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M1251.77,-103.882C1268.15,-93.9191 1285.63,-83.2862 1301.83,-73.4299\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"1303.96,-76.2308 1310.68,-68.0433 1300.32,-70.2504 1303.96,-76.2308\"/>\r\n",
       "</g>\r\n",
       "<!-- 17 -->\r\n",
       "<g id=\"node18\" class=\"node\"><title>17</title>\r\n",
       "<path fill=\"#b78ff0\" stroke=\"black\" d=\"M1880.5,-306C1880.5,-306 1725.5,-306 1725.5,-306 1719.5,-306 1713.5,-300 1713.5,-294 1713.5,-294 1713.5,-235 1713.5,-235 1713.5,-229 1719.5,-223 1725.5,-223 1725.5,-223 1880.5,-223 1880.5,-223 1886.5,-223 1892.5,-229 1892.5,-235 1892.5,-235 1892.5,-294 1892.5,-294 1892.5,-300 1886.5,-306 1880.5,-306\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"1803\" y=\"-290.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">第二高频计费点 &lt;= 1.5</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1803\" y=\"-275.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.501</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1803\" y=\"-260.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 4937</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1803\" y=\"-245.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [1070, 602, 3265]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1803\" y=\"-230.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 3</text>\r\n",
       "</g>\r\n",
       "<!-- 16&#45;&gt;17 -->\r\n",
       "<g id=\"edge17\" class=\"edge\"><title>16&#45;&gt;17</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M1803,-341.907C1803,-333.649 1803,-324.864 1803,-316.302\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"1806.5,-316.021 1803,-306.021 1799.5,-316.021 1806.5,-316.021\"/>\r\n",
       "</g>\r\n",
       "<!-- 24 -->\r\n",
       "<g id=\"node25\" class=\"node\"><title>24</title>\r\n",
       "<path fill=\"#8f50e8\" stroke=\"black\" d=\"M2403.5,-306C2403.5,-306 2256.5,-306 2256.5,-306 2250.5,-306 2244.5,-300 2244.5,-294 2244.5,-294 2244.5,-235 2244.5,-235 2244.5,-229 2250.5,-223 2256.5,-223 2256.5,-223 2403.5,-223 2403.5,-223 2409.5,-223 2415.5,-229 2415.5,-235 2415.5,-235 2415.5,-294 2415.5,-294 2415.5,-300 2409.5,-306 2403.5,-306\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"2330\" y=\"-290.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">最高频计费点fre &lt;= 0.835</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2330\" y=\"-275.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.191</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2330\" y=\"-260.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 4901</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2330\" y=\"-245.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [387, 126, 4388]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2330\" y=\"-230.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 3</text>\r\n",
       "</g>\r\n",
       "<!-- 16&#45;&gt;24 -->\r\n",
       "<g id=\"edge24\" class=\"edge\"><title>16&#45;&gt;24</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M1892.7,-362.586C1987.72,-341.491 2137.17,-308.311 2234.38,-286.729\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"2235.2,-290.132 2244.2,-284.548 2233.68,-283.298 2235.2,-290.132\"/>\r\n",
       "</g>\r\n",
       "<!-- 18 -->\r\n",
       "<g id=\"node19\" class=\"node\"><title>18</title>\r\n",
       "<path fill=\"#b990f0\" stroke=\"black\" d=\"M1782,-187C1782,-187 1636,-187 1636,-187 1630,-187 1624,-181 1624,-175 1624,-175 1624,-116 1624,-116 1624,-110 1630,-104 1636,-104 1636,-104 1782,-104 1782,-104 1788,-104 1794,-110 1794,-116 1794,-116 1794,-175 1794,-175 1794,-181 1788,-187 1782,-187\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"1709\" y=\"-171.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">累计消费额 &lt;= 109.845</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1709\" y=\"-156.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.455</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1709\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 2831</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1709\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [794, 106, 1931]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1709\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 3</text>\r\n",
       "</g>\r\n",
       "<!-- 17&#45;&gt;18 -->\r\n",
       "<g id=\"edge18\" class=\"edge\"><title>17&#45;&gt;18</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M1770.39,-222.907C1763.17,-213.923 1755.45,-204.315 1748.01,-195.05\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"1750.55,-192.624 1741.56,-187.021 1745.09,-197.009 1750.55,-192.624\"/>\r\n",
       "</g>\r\n",
       "<!-- 21 -->\r\n",
       "<g id=\"node22\" class=\"node\"><title>21</title>\r\n",
       "<path fill=\"#bd98f1\" stroke=\"black\" d=\"M1970,-187C1970,-187 1824,-187 1824,-187 1818,-187 1812,-181 1812,-175 1812,-175 1812,-116 1812,-116 1812,-110 1818,-104 1824,-104 1824,-104 1970,-104 1970,-104 1976,-104 1982,-110 1982,-116 1982,-116 1982,-175 1982,-175 1982,-181 1976,-187 1970,-187\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"1897\" y=\"-171.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">上次的计费点 &lt;= 2.5</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1897\" y=\"-156.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.526</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1897\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 2106</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1897\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [276, 496, 1334]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1897\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 3</text>\r\n",
       "</g>\r\n",
       "<!-- 17&#45;&gt;21 -->\r\n",
       "<g id=\"edge21\" class=\"edge\"><title>17&#45;&gt;21</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M1835.61,-222.907C1842.83,-213.923 1850.55,-204.315 1857.99,-195.05\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"1860.91,-197.009 1864.44,-187.021 1855.45,-192.624 1860.91,-197.009\"/>\r\n",
       "</g>\r\n",
       "<!-- 19 -->\r\n",
       "<g id=\"node20\" class=\"node\"><title>19</title>\r\n",
       "<path fill=\"#e0cef9\" stroke=\"black\" d=\"M1602,-68C1602,-68 1472,-68 1472,-68 1466,-68 1460,-62 1460,-56 1460,-56 1460,-12 1460,-12 1460,-6 1466,-0 1472,-0 1472,-0 1602,-0 1602,-0 1608,-0 1614,-6 1614,-12 1614,-12 1614,-56 1614,-56 1614,-62 1608,-68 1602,-68\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"1537\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.527</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1537\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 601</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1537\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [244, 24, 333]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1537\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 3</text>\r\n",
       "</g>\r\n",
       "<!-- 18&#45;&gt;19 -->\r\n",
       "<g id=\"edge19\" class=\"edge\"><title>18&#45;&gt;19</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M1644.95,-103.726C1629.54,-93.9161 1613.13,-83.4644 1597.87,-73.7496\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"1599.35,-70.5431 1589.03,-68.1252 1595.59,-76.4481 1599.35,-70.5431\"/>\r\n",
       "</g>\r\n",
       "<!-- 20 -->\r\n",
       "<g id=\"node21\" class=\"node\"><title>20</title>\r\n",
       "<path fill=\"#b083ef\" stroke=\"black\" d=\"M1782,-68C1782,-68 1644,-68 1644,-68 1638,-68 1632,-62 1632,-56 1632,-56 1632,-12 1632,-12 1632,-6 1638,-0 1644,-0 1644,-0 1782,-0 1782,-0 1788,-0 1794,-6 1794,-12 1794,-12 1794,-56 1794,-56 1794,-62 1788,-68 1782,-68\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"1713\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.424</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1713\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 2230</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1713\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [550, 82, 1598]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1713\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 3</text>\r\n",
       "</g>\r\n",
       "<!-- 18&#45;&gt;20 -->\r\n",
       "<g id=\"edge20\" class=\"edge\"><title>18&#45;&gt;20</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M1710.49,-103.726C1710.79,-95.5175 1711.11,-86.8595 1711.41,-78.56\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"1714.92,-78.4207 1711.78,-68.2996 1707.92,-78.1651 1714.92,-78.4207\"/>\r\n",
       "</g>\r\n",
       "<!-- 22 -->\r\n",
       "<g id=\"node23\" class=\"node\"><title>22</title>\r\n",
       "<path fill=\"#dfccf8\" stroke=\"black\" d=\"M1962,-68C1962,-68 1824,-68 1824,-68 1818,-68 1812,-62 1812,-56 1812,-56 1812,-12 1812,-12 1812,-6 1818,-0 1824,-0 1824,-0 1962,-0 1962,-0 1968,-0 1974,-6 1974,-12 1974,-12 1974,-56 1974,-56 1974,-62 1968,-68 1962,-68\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"1893\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.604</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1893\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 781</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1893\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [120, 264, 397]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"1893\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 3</text>\r\n",
       "</g>\r\n",
       "<!-- 21&#45;&gt;22 -->\r\n",
       "<g id=\"edge22\" class=\"edge\"><title>21&#45;&gt;22</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M1895.51,-103.726C1895.21,-95.5175 1894.89,-86.8595 1894.59,-78.56\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"1898.08,-78.1651 1894.22,-68.2996 1891.08,-78.4207 1898.08,-78.1651\"/>\r\n",
       "</g>\r\n",
       "<!-- 23 -->\r\n",
       "<g id=\"node24\" class=\"node\"><title>23</title>\r\n",
       "<path fill=\"#ae7fee\" stroke=\"black\" d=\"M2142,-68C2142,-68 2004,-68 2004,-68 1998,-68 1992,-62 1992,-56 1992,-56 1992,-12 1992,-12 1992,-6 1998,-0 2004,-0 2004,-0 2142,-0 2142,-0 2148,-0 2154,-6 2154,-12 2154,-12 2154,-56 2154,-56 2154,-62 2148,-68 2142,-68\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"2073\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.455</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2073\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 1325</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2073\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [156, 232, 937]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2073\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 3</text>\r\n",
       "</g>\r\n",
       "<!-- 21&#45;&gt;23 -->\r\n",
       "<g id=\"edge23\" class=\"edge\"><title>21&#45;&gt;23</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M1962.54,-103.726C1978.45,-93.8245 1995.42,-83.269 2011.16,-73.4774\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"2013.12,-76.3797 2019.76,-68.1252 2009.42,-70.4361 2013.12,-76.3797\"/>\r\n",
       "</g>\r\n",
       "<!-- 25 -->\r\n",
       "<g id=\"node26\" class=\"node\"><title>25</title>\r\n",
       "<path fill=\"#965ae9\" stroke=\"black\" d=\"M2403,-187C2403,-187 2257,-187 2257,-187 2251,-187 2245,-181 2245,-175 2245,-175 2245,-116 2245,-116 2245,-110 2251,-104 2257,-104 2257,-104 2403,-104 2403,-104 2409,-104 2415,-110 2415,-116 2415,-116 2415,-175 2415,-175 2415,-181 2409,-187 2403,-187\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"2330\" y=\"-171.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">累计消费额 &lt;= 176.28</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2330\" y=\"-156.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.263</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2330\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 2960</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2330\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [333, 110, 2517]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2330\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 3</text>\r\n",
       "</g>\r\n",
       "<!-- 24&#45;&gt;25 -->\r\n",
       "<g id=\"edge25\" class=\"edge\"><title>24&#45;&gt;25</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M2330,-222.907C2330,-214.649 2330,-205.864 2330,-197.302\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"2333.5,-197.021 2330,-187.021 2326.5,-197.021 2333.5,-197.021\"/>\r\n",
       "</g>\r\n",
       "<!-- 28 -->\r\n",
       "<g id=\"node29\" class=\"node\"><title>28</title>\r\n",
       "<path fill=\"#8640e6\" stroke=\"black\" d=\"M2674.5,-187C2674.5,-187 2527.5,-187 2527.5,-187 2521.5,-187 2515.5,-181 2515.5,-175 2515.5,-175 2515.5,-116 2515.5,-116 2515.5,-110 2521.5,-104 2527.5,-104 2527.5,-104 2674.5,-104 2674.5,-104 2680.5,-104 2686.5,-110 2686.5,-116 2686.5,-116 2686.5,-175 2686.5,-175 2686.5,-181 2680.5,-187 2674.5,-187\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"2601\" y=\"-171.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">距上次付费的天数 &lt;= 94.5</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2601\" y=\"-156.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.07</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2601\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 1941</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2601\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [54, 16, 1871]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2601\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 3</text>\r\n",
       "</g>\r\n",
       "<!-- 24&#45;&gt;28 -->\r\n",
       "<g id=\"edge28\" class=\"edge\"><title>24&#45;&gt;28</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M2415.75,-226.48C2444.43,-214.096 2476.57,-200.221 2505.92,-187.548\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"2507.55,-190.656 2515.35,-183.479 2504.78,-184.23 2507.55,-190.656\"/>\r\n",
       "</g>\r\n",
       "<!-- 26 -->\r\n",
       "<g id=\"node27\" class=\"node\"><title>26</title>\r\n",
       "<path fill=\"#ae80ee\" stroke=\"black\" d=\"M2314,-68C2314,-68 2184,-68 2184,-68 2178,-68 2172,-62 2172,-56 2172,-56 2172,-12 2172,-12 2172,-6 2178,-0 2184,-0 2184,-0 2314,-0 2314,-0 2320,-0 2326,-6 2326,-12 2326,-12 2326,-56 2326,-56 2326,-62 2320,-68 2314,-68\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"2249\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.45</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2249\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 664</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2249\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [128, 65, 471]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2249\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 3</text>\r\n",
       "</g>\r\n",
       "<!-- 25&#45;&gt;26 -->\r\n",
       "<g id=\"edge26\" class=\"edge\"><title>25&#45;&gt;26</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M2299.84,-103.726C2293.23,-94.7878 2286.22,-85.3168 2279.59,-76.3558\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"2282.39,-74.2576 2273.63,-68.2996 2276.77,-78.4206 2282.39,-74.2576\"/>\r\n",
       "</g>\r\n",
       "<!-- 27 -->\r\n",
       "<g id=\"node28\" class=\"node\"><title>27</title>\r\n",
       "<path fill=\"#9051e8\" stroke=\"black\" d=\"M2494,-68C2494,-68 2356,-68 2356,-68 2350,-68 2344,-62 2344,-56 2344,-56 2344,-12 2344,-12 2344,-6 2350,-0 2356,-0 2356,-0 2494,-0 2494,-0 2500,-0 2506,-6 2506,-12 2506,-12 2506,-56 2506,-56 2506,-62 2500,-68 2494,-68\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"2425\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.198</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2425\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 2296</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2425\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [205, 45, 2046]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2425\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 3</text>\r\n",
       "</g>\r\n",
       "<!-- 25&#45;&gt;27 -->\r\n",
       "<g id=\"edge27\" class=\"edge\"><title>25&#45;&gt;27</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M2365.37,-103.726C2373.21,-94.6966 2381.51,-85.1235 2389.36,-76.0816\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"2392.2,-78.1467 2396.11,-68.2996 2386.91,-73.5595 2392.2,-78.1467\"/>\r\n",
       "</g>\r\n",
       "<!-- 29 -->\r\n",
       "<g id=\"node30\" class=\"node\"><title>29</title>\r\n",
       "<path fill=\"#8540e6\" stroke=\"black\" d=\"M2666,-68C2666,-68 2536,-68 2536,-68 2530,-68 2524,-62 2524,-56 2524,-56 2524,-12 2524,-12 2524,-6 2530,-0 2536,-0 2536,-0 2666,-0 2666,-0 2672,-0 2678,-6 2678,-12 2678,-12 2678,-56 2678,-56 2678,-62 2672,-68 2666,-68\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"2601\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.066</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2601\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 1934</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2601\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [51, 15, 1868]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2601\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 3</text>\r\n",
       "</g>\r\n",
       "<!-- 28&#45;&gt;29 -->\r\n",
       "<g id=\"edge29\" class=\"edge\"><title>28&#45;&gt;29</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M2601,-103.726C2601,-95.5175 2601,-86.8595 2601,-78.56\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"2604.5,-78.2996 2601,-68.2996 2597.5,-78.2996 2604.5,-78.2996\"/>\r\n",
       "</g>\r\n",
       "<!-- 30 -->\r\n",
       "<g id=\"node31\" class=\"node\"><title>30</title>\r\n",
       "<path fill=\"#ffffff\" stroke=\"black\" d=\"M2797.5,-68C2797.5,-68 2708.5,-68 2708.5,-68 2702.5,-68 2696.5,-62 2696.5,-56 2696.5,-56 2696.5,-12 2696.5,-12 2696.5,-6 2702.5,-0 2708.5,-0 2708.5,-0 2797.5,-0 2797.5,-0 2803.5,-0 2809.5,-6 2809.5,-12 2809.5,-12 2809.5,-56 2809.5,-56 2809.5,-62 2803.5,-68 2797.5,-68\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"2753\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.612</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2753\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 7</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2753\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [3, 1, 3]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"2753\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 1</text>\r\n",
       "</g>\r\n",
       "<!-- 28&#45;&gt;30 -->\r\n",
       "<g id=\"edge30\" class=\"edge\"><title>28&#45;&gt;30</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M2657.6,-103.726C2671.02,-94.0582 2685.31,-83.767 2698.62,-74.172\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"2700.71,-76.9846 2706.78,-68.2996 2696.62,-71.305 2700.71,-76.9846\"/>\r\n",
       "</g>\r\n",
       "</g>\r\n",
       "</svg>\r\n"
      ],
      "text/plain": [
       "<graphviz.files.Source at 0x21008e59b08>"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import graphviz\n",
    "from sklearn import tree\n",
    "dot_data = tree.export_graphviz(DT\n",
    "                                ,out_file = None\n",
    "                                ,feature_names= xtrain.columns\n",
    "                                ,class_names=['1','2','3']\n",
    "                                ,filled=True\n",
    "                                ,rounded=True\n",
    "                                )\n",
    "graph = graphviz.Source(dot_data) \n",
    "graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## XGB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xgboost import XGBClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = XGBClassifier(learning_rate=0.1,\n",
    "                      n_estimators=1000,         # 树的个数--1000棵树建立xgboost\n",
    "                      max_depth=6,               # 树的深度\n",
    "                      min_child_weight = 1,      # 叶子节点最小权重\n",
    "                       gamma=0.,                  # 惩罚项中叶子结点个数前的参数\n",
    "                       subsample=0.8,             # 随机选择80%样本建立决策树\n",
    "                       colsample_btree=0.8,       # 随机选择80%特征建立决策树\n",
    "                       objective='multi:softmax', # 指定损失函数\n",
    "                     scale_pos_weight=1,        # 解决样本个数不平衡的问题\n",
    "                     random_state=27            # 随机数\n",
    "                       )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y[ytest.index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[17:40:42] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.3.0/src/learner.cc:541: \n",
      "Parameters: { colsample_btree, scale_pos_weight } might not be used.\n",
      "\n",
      "  This may not be accurate due to some parameters are only used in language bindings but\n",
      "  passed down to XGBoost core.  Or some parameters are not used but slip through this\n",
      "  verification. Please open an issue if you find above cases.\n",
      "\n",
      "\n",
      "[0]\tvalidation_0-mlogloss:1.03445\n",
      "[1]\tvalidation_0-mlogloss:0.98103\n",
      "[2]\tvalidation_0-mlogloss:0.93579\n",
      "[3]\tvalidation_0-mlogloss:0.89684\n",
      "[4]\tvalidation_0-mlogloss:0.86332\n",
      "[5]\tvalidation_0-mlogloss:0.83415\n",
      "[6]\tvalidation_0-mlogloss:0.80882\n",
      "[7]\tvalidation_0-mlogloss:0.78623\n",
      "[8]\tvalidation_0-mlogloss:0.76672\n",
      "[9]\tvalidation_0-mlogloss:0.74912\n",
      "[10]\tvalidation_0-mlogloss:0.73388\n",
      "[11]\tvalidation_0-mlogloss:0.71988\n",
      "[12]\tvalidation_0-mlogloss:0.70815\n",
      "[13]\tvalidation_0-mlogloss:0.69744\n",
      "[14]\tvalidation_0-mlogloss:0.68794\n",
      "[15]\tvalidation_0-mlogloss:0.67931\n",
      "[16]\tvalidation_0-mlogloss:0.67143\n",
      "[17]\tvalidation_0-mlogloss:0.66472\n",
      "[18]\tvalidation_0-mlogloss:0.65850\n",
      "[19]\tvalidation_0-mlogloss:0.65266\n",
      "[20]\tvalidation_0-mlogloss:0.64770\n",
      "[21]\tvalidation_0-mlogloss:0.64316\n",
      "[22]\tvalidation_0-mlogloss:0.63899\n",
      "[23]\tvalidation_0-mlogloss:0.63511\n",
      "[24]\tvalidation_0-mlogloss:0.63174\n",
      "[25]\tvalidation_0-mlogloss:0.62867\n",
      "[26]\tvalidation_0-mlogloss:0.62545\n",
      "[27]\tvalidation_0-mlogloss:0.62297\n",
      "[28]\tvalidation_0-mlogloss:0.62045\n",
      "[29]\tvalidation_0-mlogloss:0.61836\n",
      "[30]\tvalidation_0-mlogloss:0.61638\n",
      "[31]\tvalidation_0-mlogloss:0.61420\n",
      "[32]\tvalidation_0-mlogloss:0.61242\n",
      "[33]\tvalidation_0-mlogloss:0.61106\n",
      "[34]\tvalidation_0-mlogloss:0.60937\n",
      "[35]\tvalidation_0-mlogloss:0.60841\n",
      "[36]\tvalidation_0-mlogloss:0.60701\n",
      "[37]\tvalidation_0-mlogloss:0.60574\n",
      "[38]\tvalidation_0-mlogloss:0.60465\n",
      "[39]\tvalidation_0-mlogloss:0.60395\n",
      "[40]\tvalidation_0-mlogloss:0.60304\n",
      "[41]\tvalidation_0-mlogloss:0.60224\n",
      "[42]\tvalidation_0-mlogloss:0.60158\n",
      "[43]\tvalidation_0-mlogloss:0.60085\n",
      "[44]\tvalidation_0-mlogloss:0.60014\n",
      "[45]\tvalidation_0-mlogloss:0.59954\n",
      "[46]\tvalidation_0-mlogloss:0.59899\n",
      "[47]\tvalidation_0-mlogloss:0.59867\n",
      "[48]\tvalidation_0-mlogloss:0.59837\n",
      "[49]\tvalidation_0-mlogloss:0.59811\n",
      "[50]\tvalidation_0-mlogloss:0.59762\n",
      "[51]\tvalidation_0-mlogloss:0.59734\n",
      "[52]\tvalidation_0-mlogloss:0.59677\n",
      "[53]\tvalidation_0-mlogloss:0.59637\n",
      "[54]\tvalidation_0-mlogloss:0.59567\n",
      "[55]\tvalidation_0-mlogloss:0.59535\n",
      "[56]\tvalidation_0-mlogloss:0.59491\n",
      "[57]\tvalidation_0-mlogloss:0.59439\n",
      "[58]\tvalidation_0-mlogloss:0.59400\n",
      "[59]\tvalidation_0-mlogloss:0.59392\n",
      "[60]\tvalidation_0-mlogloss:0.59354\n",
      "[61]\tvalidation_0-mlogloss:0.59329\n",
      "[62]\tvalidation_0-mlogloss:0.59327\n",
      "[63]\tvalidation_0-mlogloss:0.59314\n",
      "[64]\tvalidation_0-mlogloss:0.59298\n",
      "[65]\tvalidation_0-mlogloss:0.59264\n",
      "[66]\tvalidation_0-mlogloss:0.59253\n",
      "[67]\tvalidation_0-mlogloss:0.59247\n",
      "[68]\tvalidation_0-mlogloss:0.59239\n",
      "[69]\tvalidation_0-mlogloss:0.59227\n",
      "[70]\tvalidation_0-mlogloss:0.59237\n",
      "[71]\tvalidation_0-mlogloss:0.59210\n",
      "[72]\tvalidation_0-mlogloss:0.59198\n",
      "[73]\tvalidation_0-mlogloss:0.59188\n",
      "[74]\tvalidation_0-mlogloss:0.59162\n",
      "[75]\tvalidation_0-mlogloss:0.59150\n",
      "[76]\tvalidation_0-mlogloss:0.59135\n",
      "[77]\tvalidation_0-mlogloss:0.59137\n",
      "[78]\tvalidation_0-mlogloss:0.59093\n",
      "[79]\tvalidation_0-mlogloss:0.59093\n",
      "[80]\tvalidation_0-mlogloss:0.59060\n",
      "[81]\tvalidation_0-mlogloss:0.59039\n",
      "[82]\tvalidation_0-mlogloss:0.59008\n",
      "[83]\tvalidation_0-mlogloss:0.58987\n",
      "[84]\tvalidation_0-mlogloss:0.58991\n",
      "[85]\tvalidation_0-mlogloss:0.58970\n",
      "[86]\tvalidation_0-mlogloss:0.58970\n",
      "[87]\tvalidation_0-mlogloss:0.58955\n",
      "[88]\tvalidation_0-mlogloss:0.58941\n",
      "[89]\tvalidation_0-mlogloss:0.58935\n",
      "[90]\tvalidation_0-mlogloss:0.58925\n",
      "[91]\tvalidation_0-mlogloss:0.58921\n",
      "[92]\tvalidation_0-mlogloss:0.58914\n",
      "[93]\tvalidation_0-mlogloss:0.58908\n",
      "[94]\tvalidation_0-mlogloss:0.58897\n",
      "[95]\tvalidation_0-mlogloss:0.58888\n",
      "[96]\tvalidation_0-mlogloss:0.58880\n",
      "[97]\tvalidation_0-mlogloss:0.58888\n",
      "[98]\tvalidation_0-mlogloss:0.58870\n",
      "[99]\tvalidation_0-mlogloss:0.58857\n",
      "[100]\tvalidation_0-mlogloss:0.58849\n",
      "[101]\tvalidation_0-mlogloss:0.58850\n",
      "[102]\tvalidation_0-mlogloss:0.58836\n",
      "[103]\tvalidation_0-mlogloss:0.58827\n",
      "[104]\tvalidation_0-mlogloss:0.58835\n",
      "[105]\tvalidation_0-mlogloss:0.58847\n",
      "[106]\tvalidation_0-mlogloss:0.58835\n",
      "[107]\tvalidation_0-mlogloss:0.58829\n",
      "[108]\tvalidation_0-mlogloss:0.58839\n",
      "[109]\tvalidation_0-mlogloss:0.58828\n",
      "[110]\tvalidation_0-mlogloss:0.58830\n",
      "[111]\tvalidation_0-mlogloss:0.58822\n",
      "[112]\tvalidation_0-mlogloss:0.58826\n",
      "[113]\tvalidation_0-mlogloss:0.58823\n",
      "[114]\tvalidation_0-mlogloss:0.58826\n",
      "[115]\tvalidation_0-mlogloss:0.58829\n",
      "[116]\tvalidation_0-mlogloss:0.58827\n",
      "[117]\tvalidation_0-mlogloss:0.58816\n",
      "[118]\tvalidation_0-mlogloss:0.58818\n",
      "[119]\tvalidation_0-mlogloss:0.58803\n",
      "[120]\tvalidation_0-mlogloss:0.58807\n",
      "[121]\tvalidation_0-mlogloss:0.58815\n",
      "[122]\tvalidation_0-mlogloss:0.58812\n",
      "[123]\tvalidation_0-mlogloss:0.58810\n",
      "[124]\tvalidation_0-mlogloss:0.58803\n",
      "[125]\tvalidation_0-mlogloss:0.58808\n",
      "[126]\tvalidation_0-mlogloss:0.58809\n",
      "[127]\tvalidation_0-mlogloss:0.58801\n",
      "[128]\tvalidation_0-mlogloss:0.58795\n",
      "[129]\tvalidation_0-mlogloss:0.58772\n",
      "[130]\tvalidation_0-mlogloss:0.58772\n",
      "[131]\tvalidation_0-mlogloss:0.58782\n",
      "[132]\tvalidation_0-mlogloss:0.58776\n",
      "[133]\tvalidation_0-mlogloss:0.58767\n",
      "[134]\tvalidation_0-mlogloss:0.58761\n",
      "[135]\tvalidation_0-mlogloss:0.58762\n",
      "[136]\tvalidation_0-mlogloss:0.58757\n",
      "[137]\tvalidation_0-mlogloss:0.58756\n",
      "[138]\tvalidation_0-mlogloss:0.58757\n",
      "[139]\tvalidation_0-mlogloss:0.58766\n",
      "[140]\tvalidation_0-mlogloss:0.58760\n",
      "[141]\tvalidation_0-mlogloss:0.58760\n",
      "[142]\tvalidation_0-mlogloss:0.58766\n",
      "[143]\tvalidation_0-mlogloss:0.58758\n",
      "[144]\tvalidation_0-mlogloss:0.58757\n",
      "[145]\tvalidation_0-mlogloss:0.58756\n",
      "[146]\tvalidation_0-mlogloss:0.58764\n",
      "[147]\tvalidation_0-mlogloss:0.58781\n",
      "[148]\tvalidation_0-mlogloss:0.58782\n",
      "[149]\tvalidation_0-mlogloss:0.58788\n",
      "[150]\tvalidation_0-mlogloss:0.58774\n",
      "[151]\tvalidation_0-mlogloss:0.58772\n",
      "[152]\tvalidation_0-mlogloss:0.58777\n",
      "[153]\tvalidation_0-mlogloss:0.58779\n",
      "[154]\tvalidation_0-mlogloss:0.58771\n",
      "[155]\tvalidation_0-mlogloss:0.58752\n",
      "[156]\tvalidation_0-mlogloss:0.58755\n",
      "[157]\tvalidation_0-mlogloss:0.58762\n",
      "[158]\tvalidation_0-mlogloss:0.58776\n",
      "[159]\tvalidation_0-mlogloss:0.58781\n",
      "[160]\tvalidation_0-mlogloss:0.58777\n",
      "[161]\tvalidation_0-mlogloss:0.58775\n",
      "[162]\tvalidation_0-mlogloss:0.58779\n",
      "[163]\tvalidation_0-mlogloss:0.58790\n",
      "[164]\tvalidation_0-mlogloss:0.58795\n",
      "[165]\tvalidation_0-mlogloss:0.58792\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "XGBClassifier(base_score=0.5, booster='gbtree', colsample_btree=0.8,\n",
       "              colsample_bylevel=1, colsample_bynode=1, colsample_bytree=1,\n",
       "              gamma=0.0, gpu_id=-1, importance_type='gain',\n",
       "              interaction_constraints='', learning_rate=0.1, max_delta_step=0,\n",
       "              max_depth=6, min_child_weight=1, missing=nan,\n",
       "              monotone_constraints='()', n_estimators=1000, n_jobs=16,\n",
       "              num_parallel_tree=1, objective='multi:softprob', random_state=27,\n",
       "              reg_alpha=0, reg_lambda=1, scale_pos_weight=1, subsample=0.8,\n",
       "              tree_method='exact', validate_parameters=1, verbosity=None)"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(xtrain,\n",
    "        y[ytrain.index],\n",
    "          eval_set = [(xtest,y[ytest.index])],\n",
    "          eval_metric = \"mlogloss\",\n",
    "           early_stopping_rounds = 10,\n",
    "           verbose = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7329410291724051"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.score(xtest,y[ytest.index])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "273px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
